All files / lib/icloud/icloud-photos icloud-photos.ts

99.89% Statements 946/947
99.11% Branches 223/225
100% Functions 32/32
99.89% Lines 946/947

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 9471x 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 776x 776x 776x 776x 776x 776x 776x 776x 776x 1x 1x 1x 1x 1x 1x 776x 7x 776x 776x 776x 776x 1x 1x 1x 1x 1x 1x 787x 787x 787x 787x 787x 787x 787x 787x 787x 787x 1x 1x 1x 1x 1x 1x 1x 1x 1x 11x 11x 11x 11x 11x 11x 11x 5x 5x 5x 5x 5x 9x 6x 11x 11x 11x 1x 1x 1x 1x 1x 1x 1x 16x 16x 10x 10x 1x 1x 1x 1x 1x 1x 1x 1x 1x 7x 7x 7x 7x 5x 3x 2x 4x 4x 7x 3x 3x 7x 1x 1x 1x 1x 1x 1x 1x 1x 1x 25x 21x 25x 25x 25x 8x 8x 8x 8x 13x 13x 23x 7x 7x 7x 7x 7x 5x 5x 7x 7x 7x 7x 7x 6x 23x 4x 4x 4x 2x 2x 2x 2x 2x 2x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 116x 116x 116x 131x 45x 45x 129x 131x 131x 116x 30x 30x 30x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 134x 134x 134x 134x 134x 134x 134x 134x 134x 134x 134x 134x 134x 134x 134x 134x 134x 134x 134x 134x 63x 63x 132x 132x 21x 21x 132x 132x 45x 45x 132x 132x 18x 18x 132x 132x 132x 132x 132x 132x 48x 48x 48x 48x 48x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 45x 45x 45x 45x 45x 45x 45x 45x 45x 45x 45x 45x 45x 45x 45x 45x 45x 45x 45x 45x 45x 45x 45x 45x 45x 45x 45x 45x 45x 45x 45x 45x 45x 18x 18x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 177x 225x 225x 225x 225x 159x 159x 111x 111x 48x 48x 48x 48x 225x   1x 1x 1x 1x 1x 1x 155x 155x 1x 1x 155x 1x 1x 1x 1x 1x 1x 1x 169x 47x 47x 122x 122x 169x 169x 65x 65x 57x 169x 169x 169x 169x 169x 1x 1x 1x 1x 1x 1x 1x 1x 111x 45x 45x 66x 66x 66x 66x 66x 66x 66x 111x 54x 30x 30x 54x 54x 18x 18x 111x 12x 12x 66x 111x 12x 12x 66x 66x 66x 1x 1x 1x 1x 1x 1x 1x 1x 1x 6x 6x 6x 6x 6x 6x 6x 6x 9x 9x 7x 7x 9x 9x 9x 4x 4x 4x 9x 9x 9x 9x 7x 4x 4x 6x 2x 1x 3x 3x 3x 3x 3x 6x 1x 1x 1x 1x 1x 1x 1x 1x 2x 2x 2x 1x 1x 1x 2x 2x 1x 1x 1x 1x 1x 1x 1x 11x 2x 2x 2x 2x 9x 11x 3x 3x 3x 3x 6x 6x 11x 1x 1x 1x 1x 11x 1x 1x 1x 1x 1x 1x 1x 1x 2x 2x 2x 5x 5x 5x 5x 2x 2x 2x 2x 5x 1x 1x 5x 3x 3x 5x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 2x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 3x 3x 3x 3x 3x 3x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 10x 10x 10x 10x 10x 10x 10x 8x 10x 4x 4x 4x 4x 10x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 18x 18x 18x 18x 10x 10x 10x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 24x 24x 24x 24x 24x 24x 24x 48x 48x 48x 16x 16x 48x 48x 48x 48x 48x 48x 48x 48x 48x 48x 48x 45x 45x 48x 6x 6x 6x 39x 39x 39x 48x 24x 48x 15x 15x 15x 48x 21x 21x 21x 1x 1x 1x 1x 1x 1x 1x 1x 52x 52x 1x 1x 1x 1x 1x 1x 1x 1x 80x 3x 3x 3x 77x 80x 4x 4x 4x 73x 73x 80x 3x 3x 3x 70x 80x 6x 6x 6x 6x 64x 64x 80x 3x 3x 3x 3x 80x 1x 1x 1x 1x 1x 1x 1x 1x 1x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 26x 26x 4x 4x 22x 22x 22x 22x 6x 6x 6x 6x 1x 1x 1x 1x 1x 1x 1x 1x 1x 11x 11x 11x 11x 11x 11x 9x 9x 11x 2x 2x 2x 2x 2x 9x 9x 11x 1x 1x 1x 1x 11x 2x 2x 2x 2x 9x 11x 11x 1x 1x 1x 1x 1x 1x 1x 1x 4x 4x 4x 3x 4x 1x 1x 1x 1x 4x 1x 1x 1x 1x 1x 1x 5x 4x 5x 2x 2x 2x 2x 2x 4x 4x 4x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 12x 12x 12x 12x 12x 12x 12x 12x 12x 71x 71x 71x 71x 28x 28x 28x 57x 71x 29x 29x 29x 71x 16x 3x 3x 1x 1x 3x 16x 16x 16x 16x 71x 12x 12x 12x 3x 3x 3x 3x 12x 12x 12x 5x 5x 5x 2x 2x 5x 12x 12x 12x 3x 3x 3x 3x 3x 3x 12x 9x 9x 12x 12x 12x 1x 1x 1x 1x 1x 1x 1x 1x 3x 1x 1x 1x 2x 2x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 2x 2x 1x 1x
import {jsonc} from 'jsonc';
import {ICLOUD_PHOTOS_ERR} from '../../../app/error/error-codes.js';
import {errorMessage, iCPSError} from '../../../app/error/error.js';
import {AlbumAssets, AlbumType} from '../../photos-library/model/album.js';
import {Asset} from '../../photos-library/model/asset.js';
import {iCPSEventPhotos, iCPSEventRuntimeWarning} from '../../resources/events-types.js';
import {HttpRequestConfig, isHttpError} from '../../resources/http-client.js';
import {Resources} from '../../resources/main.js';
import {ENDPOINTS, PhotosSetupResponseZone} from '../../resources/network-types.js';
import {SyncEngineHelper} from '../../sync-engine/helper.js';
import * as QueryBuilder from './query-builder.js';
import {CPLAlbum, CPLAsset, CPLMaster} from './query-parser.js';
import {ZoneArea} from '../../resources/resource-types.js';
import {HIDDEN_ALBUM_NAME, HIDDEN_ALBUM_UUID} from '../../photos-library/constants.js';
 
/**
 * To perform an operation, a record change tag is required. Hardcoding it for now
 */
const RECORD_CHANGE_TAG = `21h2`;
 
/**
 * The record limit requested per query.
 * iCloud returns at most 200 records per request, but in order to divide by 3 (for albums) and 2 (for all pictures) 198 is more convenient.
 * Requests are only planned based on this limit - if iCloud returns fewer records, the missing records are requested again.
 */
const MAX_RECORDS_LIMIT = 198;
 
/**
 * A single page of query results
 */
export type QueryPage = {
    /**
     * The records returned by the backend
     */
    records: any[],
    /**
     * The marker to request the next page of the same query, if more results are available
     */
    continuationMarker?: string,
}
 
/**
 * The maximum length of a non-JSON response body included in an error message
 */
const MAX_ERROR_BODY_LENGTH = 200;
 
/**
 * CloudKit server error codes, asking the client to retry the request after the provided amount of time
 */
const RETRYABLE_SERVER_ERROR_CODES = [`THROTTLED`, `TRY_AGAIN_LATER`];
 
/**
 * The number of times a single request is retried, after CloudKit asked to retry it later
 */
const MAX_THROTTLED_RETRIES = 10;
 
/**
 * The time in seconds to wait before retrying a request, if CloudKit did not provide a valid one
 */
const DEFAULT_RETRY_AFTER = 10;
 
/**
 * The maximum time in seconds to wait before retrying a request
 */
const MAX_RETRY_AFTER = 300;
 
/**
 * This class holds connection and state with the iCloud Photos Backend and provides functions to access the data stored there
 */
export class iCloudPhotos {
    /**
     * A promise that will resolve, once the object is ready or reject, in case there is an error
     */
    ready: Promise<void>;
 
    /**
     * The point in time (in ms since epoch) until which CloudKit asked to pause requests
     */
    _throttledUntil: number = 0;
 
    /**
     * Whether iCloud reported a running indexing job for any zone during the last setup - in this case the remote state might be incomplete
     */
    indexingInProgress: boolean = false;
 
    /**
     * Creates a new iCloud Photos Class
     */
    constructor() {
        Resources.events(this).on(iCPSEventPhotos.SETUP_COMPLETED, async () => {
            await this.checkingIndexingStatus();
        });
 
        this.ready = this.getReady();
    }
 
    /**
     * Listeners of a previously created promise are removed, so that a stale (already settled) promise does not consume the events of a later setup
     * @returns - A promise, that will resolve once this objects emits 'READY' or reject if it emits 'ERROR'
     */
    getReady(): Promise<void> {
        Resources.events(this)
            .removeListeners(iCPSEventPhotos.READY)
            .removeListeners(iCPSEventPhotos.ERROR);
 
        return new Promise<void>((resolve, reject) => {
            Resources.events(this)
                .once(iCPSEventPhotos.READY, () => resolve())
                .once(iCPSEventPhotos.ERROR, err => reject(err));
        });
    }
 
    /**
     * Starting iCloud Photos service, acquiring all necessary account information required to interact with the backend. This includes information about a shared library
     * Will emit SETUP_COMPLETE or ERROR
     * @returns A promise, that will resolve once the service is available or reject in case of an error
     * @emits iCPSEventPhotos.SETUP_COMPLETED - Once the setup is completed
     * @emits iCPSEventPhotos.ERROR - In case of an error during setup - The iCPSError is provided as argument
     */
    async setup() {
        // The setup is repeated when re-establishing the connection (e.g. on sync retries), the result of a previous setup must not be re-used
        this.ready = this.getReady();
        try {
            Resources.logger(this).debug(`Getting iCloud Photos account information`);
 
            Resources.network().applyZones(
                await this.getZonesInArea(`PRIVATE`),
                await this.getZonesInArea(`SHARED`)
            )
 
            Resources.logger(this).debug(`Successfully gathered iCloud Photos account information`);
            Resources.emit(iCPSEventPhotos.SETUP_COMPLETED);
        } catch (err) {
            Resources.emit(iCPSEventPhotos.ERROR, new iCPSError(ICLOUD_PHOTOS_ERR.SETUP_ERROR).addCause(err));
        } 
        return this.ready;
    }
 
    /**
     * Checks for everyone zone available in the respective area
     * @param area Either private or shared - depending on the ownership of the area
     * @returns An array of zone references
     */
    private async getZonesInArea(area: ZoneArea): Promise<PhotosSetupResponseZone[]> {
        Resources.logger(this).debug(`Getting zones in ${area} area`);
        const validatedResponse = await Resources.network().post(ENDPOINTS.PHOTOS.AREAS[area] + ENDPOINTS.PHOTOS.PATH.ZONES, {}, Resources.validator().response.photosSetup);
        return validatedResponse.data.zones;
    }
 
    /**
     * Checking indexing state of all available zones of the photos service
     * A running indexing job does not block the sync, since iCloud reports stale jobs as running while all records are available - instead `indexingInProgress` is set, so that local deletions can be deferred
     * @emits iCPSEventPhotos.READY - If the indexing state could be read
     * @emits iCPSEventPhotos.ERROR - If the indexing state is unavailable or unknown - The iCPSError is provided as argument
     * Will emit READY, or ERROR
     */
    async checkingIndexingStatus() {
        Resources.logger(this).debug(`Checking Indexing Status of iCloud Photos Account`);
        this.indexingInProgress = false;
        try {
            await this.checkIndexingStatusForZone(QueryBuilder.Zones.Primary);
            if (Resources.manager().sharedZoneAvailable) {
                await this.checkIndexingStatusForZone(QueryBuilder.Zones.Shared);
            }
 
            Resources.emit(iCPSEventPhotos.READY);
        } catch (err) {
            Resources.emit(iCPSEventPhotos.ERROR, new iCPSError(ICLOUD_PHOTOS_ERR.INDEXING_STATE_UNAVAILABLE).addCause(err));
        }
    }
 
    /**
     * Checks the indexing status of a given zone.
     * @param zone - The zone to check
     * @returns If indexing is finished or still running
     * @throws An iCPSError, if the indexing state is unavailable or unknown
     * @emits iCPSEventRuntimeWarning.INDEXING_IN_PROGRESS - If indexing is still running - provides the iCPSError (including zone and progress) as argument
     */
    async checkIndexingStatusForZone(zone: QueryBuilder.Zones) {
        const result = await this.performQuery(zone, `CheckIndexingState`);
 
        const indexingState = result[0]?.fields?.state?.value as string;
 
        if (!indexingState) {
            throw new iCPSError(ICLOUD_PHOTOS_ERR.INDEXING_STATE_UNAVAILABLE)
                .addMessage(`zone: ${zone}`)
                .addContext(`icloudResult`, result);
        }
 
        // iCloud reports stale indexing jobs as running (with progress 0), while all records are available (observed 2026-10) - therefore not blocking the sync
        if (indexingState === `RUNNING`) {
            const indexingInProgressWarning = new iCPSError(ICLOUD_PHOTOS_ERR.INDEXING_IN_PROGRESS)
                .addMessage(`zone: ${zone}`);
 
            const progress = result[0]?.fields?.progress?.value;
            if (progress !== undefined) {
                indexingInProgressWarning.addMessage(`progress ${progress}`);
            }
 
            this.indexingInProgress = true;
            Resources.emit(iCPSEventRuntimeWarning.INDEXING_IN_PROGRESS, indexingInProgressWarning);
            return;
        }
 
        if (indexingState === `FINISHED`) {
            Resources.logger(this).info(`Indexing of ${zone} finished, sync can start!`);
            return;
        }
 
        throw new iCPSError(ICLOUD_PHOTOS_ERR.INDEXING_STATE_UNKNOWN)
            .addContext(`icloudResult`, result)
            .addMessage(`zone: ${zone}`)
            .addMessage(`indexing state: ${indexingState}`);
    }
 
    /**
     * Performs a query against the iCloud Photos Service, following the continuation marker until all results are fetched
     * @param zone - Defines the zone to be used
     * @param recordType - The requested record type
     * @param filterBy - An array of filter instructions
     * @param resultsLimit - The number of records requested per page, iCloud returns at most 200 records per request
     * @param desiredKeys - The fields requested from the backend
     * @returns An array of records as returned by the backend
     * @throws An iCPSError, if the request was rejected (containing the CloudKit error details) or the response is invalid - an HttpError, if no response was received
     */
    async performQuery(zone: QueryBuilder.Zones, recordType: string, filterBy?: any[], resultsLimit?: number, desiredKeys?: string[]): Promise<any[]> {
        const records: any[] = [];
        let continuationMarker: string | undefined;
        do {
            const page = await this.performQueryPage(zone, recordType, filterBy, resultsLimit, desiredKeys, continuationMarker);
            records.push(...page.records);
            // An empty page or a repeated marker would never finish the query
            continuationMarker = page.records.length > 0 && page.continuationMarker !== continuationMarker
                ? page.continuationMarker
                : undefined;
        } while (continuationMarker);
 
        return records;
    }
 
    /**
     * Performs a single query request against the iCloud Photos Service
     * @param zone - Defines the zone to be used
     * @param recordType - The requested record type
     * @param filterBy - An array of filter instructions
     * @param resultsLimit - The number of records requested, iCloud returns at most 200 records per request
     * @param desiredKeys - The fields requested from the backend
     * @param continuationMarker - The marker returned by the previous page of the same query, in order to request the next page
     * @returns The records as returned by the backend, as well as the marker for the next page if more results are available
     * @throws An iCPSError, if the request was rejected (containing the CloudKit error details) or the response is invalid - an HttpError, if no response was received
     */
    async performQueryPage(zone: QueryBuilder.Zones, recordType: string, filterBy?: any[], resultsLimit?: number, desiredKeys?: string[], continuationMarker?: string): Promise<QueryPage> {
        const config: HttpRequestConfig = {
            params: {
                remapEnums: `True`,
            },
        };
 
        const zoneId = QueryBuilder.getZoneID(zone)
 
        const data: any = {
            query: {
                recordType: `${recordType}`,
            },
            zoneID: {
                zoneName: zoneId.zoneName,
                zoneType: zoneId.zoneType,
                ownerRecordName: zoneId.ownerRecordName,
            }
        };
 
        if (filterBy) {
            data.query.filterBy = filterBy;
        }
 
        if (desiredKeys) {
            data.desiredKeys = desiredKeys;
        }
 
        if (resultsLimit) {
            data.resultsLimit = resultsLimit;
        }
 
        if (continuationMarker) {
            data.continuationMarker = continuationMarker;
        }
 
        // Only the response format is schema validated, the records are parsed defensively
        const queryResponse = await this.sendRequest(
            () => Resources.network().post(ENDPOINTS.PHOTOS.AREAS[zoneId.area] + ENDPOINTS.PHOTOS.PATH.QUERY, data, Resources.validator().response.query, config),
            `${recordType} query`,
        );
        return {
            records: queryResponse.data.records,
            continuationMarker: queryResponse.data.continuationMarker,
        };
    }
 
    /**
     * Performs a single operation with the iCloud Backend
     * @param zone - Defines the zone to be used
     * @param operationType - The type of operation, that should be performed
     * @param recordNames - The list of recordNames of the asset the operation should be performed on
     * @param fields - The fields to be altered
     * @returns An array of records that have been altered
     * @throws An iCPSError, if the request was rejected (containing the CloudKit error details) or the response is invalid - an HttpError, if no response was received
     */
    async performOperation(zone: QueryBuilder.Zones, operationType: string, fields: any, recordNames: string[]): Promise<any[]> {
        const config: HttpRequestConfig = {
            params: {
                remapEnums: `True`,
            },
        };
 
        const zoneId = QueryBuilder.getZoneID(zone)
 
        const data: any = {
            operations: [],
            zoneID: {
                zoneName: zoneId.zoneName,
                zoneType: zoneId.zoneType,
                ownerRecordName: zoneId.ownerRecordName,
            },
            atomic: true,
        };
 
        data.operations = recordNames.map(recordName => ({
            operationType: `${operationType}`,
            record: {
                recordName: `${recordName}`,
                recordType: `CPLAsset`,
                recordChangeTag: RECORD_CHANGE_TAG,
                fields,
            },
        }));
 
        // Only the response format is schema validated, the records are parsed defensively
        const operationResponse = await this.sendRequest(
            () => Resources.network().post(ENDPOINTS.PHOTOS.AREAS[zoneId.area] + ENDPOINTS.PHOTOS.PATH.MODIFY, data, Resources.validator().response.operation, config),
            `${operationType} operation`,
        );
        return operationResponse.data.records;
    }
 
    /**
     * Sends a request to the iCloud Photos Service, retrying it in case CloudKit asks to retry later (e.g. because the client is throttled).
     * Throttling applies to all requests: While waiting, all other requests sent through this function are paused as well.
     * @param request - A function sending the request
     * @param description - A description of the request, used in log and error messages
     * @returns The response of the request
     * @throws An iCPSError, if the request was rejected (containing the CloudKit error details) or the response is invalid - an HttpError, if no response was received
     */
    async sendRequest<T>(request: () => Promise<T>, description: string): Promise<T> {
        for (let retry = 0; ; retry++) {
            await this.awaitThrottling();
            try {
                return await request();
            } catch (err) {
                const retryAfter = this.getRetryAfter(err);
                if (retryAfter === undefined || retry >= MAX_THROTTLED_RETRIES) {
                    throw this.wrapRequestError(err, description);
                }
 
                Resources.logger(this).warn(`CloudKit asked to retry ${description} after ${retryAfter}s (retry ${retry + 1}/${MAX_THROTTLED_RETRIES})`);
                this._throttledUntil = Math.max(this._throttledUntil, Date.now() + (retryAfter * 1000));
            }
        }
    }
 
    /**
     * Waits until the throttling period requested by CloudKit has passed
     * @returns A promise that resolves, once requests can be sent again
     */
    async awaitThrottling() {
        const delay = this._throttledUntil - Date.now();
        if (delay > 0) {
            await new Promise(resolve => setTimeout(resolve, delay));
        }
    }
 
    /**
     * Determines if CloudKit asked to retry a failed request later and how long to wait
     * @param err - The error thrown by the request
     * @returns The time in seconds to wait before retrying the request, or undefined if the request should not be retried
     */
    getRetryAfter(err: unknown): number | undefined {
        if (!isHttpError(err) || err.response === undefined) {
            return undefined;
        }
 
        const {status, headers, data} = err.response;
        const serverErrorCode = typeof data === `object` && data !== null ? data.serverErrorCode : undefined;
        if (status !== 429 && !RETRYABLE_SERVER_ERROR_CODES.includes(serverErrorCode)) {
            return undefined;
        }
 
        const retryAfter = Number(data?.retryAfter ?? headers[`retry-after`]);
        return Number.isFinite(retryAfter) && retryAfter >= 0
            ? Math.min(retryAfter, MAX_RETRY_AFTER)
            : DEFAULT_RETRY_AFTER;
    }
 
    /**
     * Attaches the error details provided by CloudKit to a failed request, since the HTTP status alone does not explain the failure
     * @param err - The error thrown by the request
     * @param request - A description of the failed request
     * @returns An iCPSError containing the CloudKit error details, if the request received a response - the provided error otherwise
     */
    wrapRequestError(err: unknown, request: string): unknown {
        if (!isHttpError(err) || err.response === undefined) {
            return err;
        }
 
        const {status, headers, data, text} = err.response;
        const wrappedError = new iCPSError(ICLOUD_PHOTOS_ERR.REQUEST_FAILED)
            .addMessage(request)
            .addMessage(`status ${status}`)
            .addCause(err);
 
        if (typeof data === `object` && data !== null) {
            if (data.serverErrorCode !== undefined || data.reason !== undefined) {
                wrappedError.addMessage(`${data.serverErrorCode ?? `unknown error`}: ${data.reason ?? `no reason provided`}`);
            }
 
            if (data.retryAfter !== undefined) {
                wrappedError.addMessage(`retry after ${data.retryAfter}s`);
            }
        } else if (text.trim().length > 0) {
            wrappedError.addMessage(`response: ${text.replace(/\s+/g, ` `).trim().slice(0, MAX_ERROR_BODY_LENGTH)}`);
        }
 
        if (headers[`retry-after`] !== undefined) {
            wrappedError.addMessage(`retry-after header ${headers[`retry-after`]}`);
        }
 
        return wrappedError.addContext(`responseBody`, text.slice(0, 10 * MAX_ERROR_BODY_LENGTH));
    }
 
    /**
     * Fetches all album records, traversing the directory tree
     * @remarks Since the shared library currently does not support it's own directory tree / WebUI does not show pictures in folders we only do this for the primary zone
     *          Since we are requesting them based on parent folder and are starting from the root folder the results array should yield: If folder A is closer to the root than folder B, the index of A is smaller than the index of B
     * @returns An array of all album records in the account
     * @throws An iCPSError if fetching fails
     */
    async fetchAllCPLAlbums(): Promise<CPLAlbum[]> {
        try {
            // Final list of all albums
            const albumRecords: CPLAlbum[] = [];
 
            // Folders of the current level of the directory tree, starting with the root folder
            let currentLevel: (string | undefined)[] = [undefined];
 
            while (currentLevel.length > 0) {
                // Fetching a whole level at once and waiting for all requests to settle - this way no request is left running unobserved, in case one of them fails
                const levelAlbums = (await SyncEngineHelper.settleAll(currentLevel.map(folderId => this.fetchCPLAlbums(folderId)))).flat();
 
                currentLevel = [];
                for (const nextAlbum of levelAlbums) {
                    // If album is a folder, there is stuff in there, adding it to the next level
                    if (nextAlbum.albumType === AlbumType.FOLDER) {
                        Resources.logger(this).debug(`Adding child elements of ${nextAlbum.albumNameEnc} to the processing queue`);
                        currentLevel.push(nextAlbum.recordName);
                    }
 
                    // Adding completed album
                    albumRecords.push(nextAlbum);
                }
            }
 
            // The 'Hidden' album is not part of the album tree, adding it as top level album
            if (Resources.manager().syncHidden) {
                albumRecords.push(await this.fetchHiddenCPLAlbum());
            }
 
            return albumRecords;
        } catch (err) {
            throw new iCPSError(ICLOUD_PHOTOS_ERR.FOLDER_STRUCTURE).addCause(err);
        }
    }
 
    /**
     * Builds the request to receive all albums and folders for the given folder from the iCloud backend
     * @remarks Since the shared library currently does not support it's own directory tree / WebUI does not show pictures in folders we only do this for the primary zone
     * @param folderId- The record name of the folder. If parent is undefined, all albums without parent will be returned.
     * @returns A promise, that once resolved, contains all subfolders for the provided folder
     */
    buildAlbumRecordsRequest(folderId?: string): Promise<any[]> {
        return folderId === undefined
            ? this.performQuery(QueryBuilder.Zones.Primary, QueryBuilder.RECORD_TYPES.ALBUM_RECORDS)
            : this.performQuery(
                QueryBuilder.Zones.Primary,
                QueryBuilder.RECORD_TYPES.ALBUM_RECORDS,
                [QueryBuilder.getParentFilterForParentId(folderId)],
            );
    }
 
    /**
     * Filters unwanted picture records before post-processing
     * @param record - The record to be filtered
     * @throws An iCPSError, in case the provided record should be ignored
     */
    filterAlbumRecord(record: any) {
        if (record.deleted === true) {
            throw new iCPSError(ICLOUD_PHOTOS_ERR.DELETED_RECORD)
                .addMessage(record.recordName)
                .addContext(`record`, record);
        }
 
        if (record.recordName === `----Project-Root-Folder----` || record.recordName === `----Root-Folder----`) {
            throw new iCPSError(ICLOUD_PHOTOS_ERR.UNWANTED_ALBUM)
                .addMessage(record.recordName)
                .addContext(`record`, record);
        }
 
        if (record.fields.albumType.value !== AlbumType.FOLDER
            && record.fields.albumType.value !== AlbumType.ALBUM) {
            throw new iCPSError(ICLOUD_PHOTOS_ERR.UNKNOWN_ALBUM)
                .addMessage(record.fields.albumType.value)
                .addContext(`record.fields`, record.fields);
        }
    }
 
    /**
     * Fetching a list of albums identified by their parent.
     * @remarks Since the shared library currently does not support it's own directory tree / WebUI does not show pictures in folders we only do this for the primary zone
     * @param parentId - The record name of the parent folder. If parent is undefined, all albums without parent will be returned.
     * @returns An array of folder and album records. Unwanted folders and folder types are filtered out. Albums have their items included (as a promise)
     */
    async fetchCPLAlbums(parentId?: string): Promise<CPLAlbum[]> {
        const cplAlbums: CPLAlbum[] = [];
 
        for (const album of await this.buildAlbumRecordsRequest(parentId)) {
            try {
                this.filterAlbumRecord(album);
 
                if (album.fields.albumType.value === AlbumType.ALBUM) {
                    const [albumCPLAssets, albumCPLMasters] = await this.fetchAllCPLAssetsMasters(album.recordName);
                    cplAlbums.push(CPLAlbum.parseFromQuery(album, this.getAlbumAssets(albumCPLAssets, albumCPLMasters)));
                }
 
                if (album.fields.albumType.value === AlbumType.FOLDER) {
                    cplAlbums.push(CPLAlbum.parseFromQuery(album));
                }
            } catch (err) {
                Resources.logger(this).info(`Error processing CPLAlbum: ${jsonc.stringify(album)}: ${errorMessage(err)}`);
            }
        }
 
        return cplAlbums;
    }
 
    /**
     * Builds the album holding all hidden assets. Since this album has no representation in the iCloud backend, it is assembled from the hidden assets
     * @returns The album record of the 'Hidden' album, containing all hidden assets
     * @throws An iCPSError, in case the hidden records could not be fetched
     */
    async fetchHiddenCPLAlbum(): Promise<CPLAlbum> {
        const [hiddenCPLAssets, hiddenCPLMasters] = await this.fetchHiddenCPLAssetsMasters();
        const hiddenAlbum = new CPLAlbum();
        hiddenAlbum.recordName = HIDDEN_ALBUM_UUID;
        hiddenAlbum.albumType = AlbumType.ALBUM;
        hiddenAlbum.albumNameEnc = Buffer.from(HIDDEN_ALBUM_NAME, `utf8`).toString(`base64`);
        hiddenAlbum.modified = 0;
        hiddenAlbum.assets = this.getAlbumAssets(hiddenCPLAssets, hiddenCPLMasters);
        return hiddenAlbum;
    }
 
    /**
     * Maps the provided records to the assets of an album
     * @param cplAssets - The CPLAsset records of the album
     * @param cplMasters - The CPLMaster records of the album
     * @returns The assets of the album, mapping the filename in the asset folder to the filename presented to the user
     */
    getAlbumAssets(cplAssets: CPLAsset[], cplMasters: CPLMaster[]): AlbumAssets {
        const albumAssets: AlbumAssets = {};
        SyncEngineHelper.convertCPLAssets(cplAssets, cplMasters).forEach(asset => {
            albumAssets[asset.getAssetFilename()] = asset.getPrettyFilename();
        });
        return albumAssets;
    }
 
    /**
     * Returns the number of records currently present in a given album.
     * This is necessary to properly handling splitting up the record requests (keeping iCloud API limitations in mind)
     * @param zone - Defines the zone to be used
     * @param albumId - The record name of the album, if undefined all pictures will be returned
     * @param hidden - If set and albumId is undefined, the hidden pictures will be counted instead of all pictures
     * @returns The number of assets within the given album
     * @throws An iCPSError in case the count cannot be obtained
     */
    async getPictureRecordsCountForZone(zone: QueryBuilder.Zones, albumId?: string, hidden: boolean = false): Promise<number> {
        try {
            const indexCountFilter = QueryBuilder.getIndexCountFilter(albumId, hidden);
            const countData = await this.performQuery(
                zone,
                QueryBuilder.RECORD_TYPES.INDEX_COUNT,
                [indexCountFilter],
            );
            return Number.parseInt(countData[0].fields.itemCount.value, 10);
        } catch (err) {
            throw new iCPSError(ICLOUD_PHOTOS_ERR.COUNT_DATA)
                .addMessage(`zone ${zone}`)
                .addCause(err);
        }
    }
 
    /**
     * The iCloud API is limiting the amount of records that can be obtained with a single request.
     * This function will split the album into ranges of positions, based on the expected size of the album, in order to fetch them in parallel.
     * @param zone - Defines the zone to be used
     * @param expectedNumberOfRecords - The amount of records expected within the given album
     * @param albumId - The record name of the album, if undefined all pictures will be returned
     * @param hidden - If set and albumId is undefined, the hidden pictures will be requested instead of all pictures
     * @returns An array of Promises, that will resolve to arrays of picture records
     */
    buildPictureRecordsRequestsForZone(zone: QueryBuilder.Zones, expectedNumberOfRecords: number, albumId?: string, hidden: boolean = false): Promise<any[]>[] {
        // On all pictures two records per photo are returned (CPLMaster & CPLAsset), on albums three (CPLMaster, CPLAsset & CPLContainerRelation) - all of them are counted against the limit
        const positionsPerRequest = albumId === undefined
            ? Math.floor(MAX_RECORDS_LIMIT / 2)
            : Math.floor(MAX_RECORDS_LIMIT / 3);
        const numberOfRequests = Math.ceil(expectedNumberOfRecords / positionsPerRequest);
 
        Resources.logger(this).debug(`Expecting ${expectedNumberOfRecords} records for album ${albumId === undefined ? `All photos` : albumId} in ${zone} library, executing ${numberOfRequests} queries`);
 
        // Collecting all promise queries for parallel execution
        const pictureRecordsRequests: Promise<any[]>[] = [];
        for (let index = 0; index < numberOfRequests; index++) {
            const startRank = index * positionsPerRequest;
            const endRank = Math.min(startRank + positionsPerRequest, expectedNumberOfRecords);
            pictureRecordsRequests.push(this.fetchPictureRecordsRange(zone, startRank, endRank, albumId, hidden));
        }
 
        return pictureRecordsRequests;
    }
 
    /**
     * Fetches the picture records within a range of positions of an album.
     * If iCloud returns fewer records than requested (e.g. because it lowered its record limit), the remaining positions are requested again:
     * Following the continuation marker if one was provided, otherwise starting the query at the first position that was not yet received.
     * @param zone - Defines the zone to be used
     * @param startRank - The first position of the range
     * @param endRank - The position after the last position of the range
     * @param albumId - The record name of the album, if undefined all pictures will be returned
     * @param hidden - If set and albumId is undefined, the hidden pictures will be requested instead of all pictures
     * @returns The picture records within the range, as returned by the backend - might include records of re-requested positions or positions beyond the range
     * @throws An iCPSError, if a query fails
     */
    async fetchPictureRecordsRange(zone: QueryBuilder.Zones, startRank: number, endRank: number, albumId?: string, hidden: boolean = false): Promise<any[]> {
        const records: any[] = [];
 
        // The continuation marker is only valid for the query it was returned by, therefore the start rank of that query is retained while following it
        let queryRank = startRank;
        let receivedRank = startRank;
        let continuationMarker: string | undefined;
        while (receivedRank < endRank) {
            Resources.logger(this).debug(`Building query for records of album ${albumId === undefined ? `All photos` : albumId} in ${zone} library at index ${receivedRank}${continuationMarker ? ` (continuing query at index ${queryRank})` : ``}`);
            const filters = [QueryBuilder.getStartRankFilterForStartRank(queryRank), QueryBuilder.getDirectionFilterForDirection()];
            if (albumId !== undefined) {
                filters.push(QueryBuilder.getParentFilterForParentId(albumId));
            }
 
            const page = await this.performQueryPage(
                zone,
                albumId !== undefined
                    ? QueryBuilder.RECORD_TYPES.PHOTO_RECORDS
                    : (hidden ? QueryBuilder.RECORD_TYPES.HIDDEN_PHOTOS : QueryBuilder.RECORD_TYPES.ALL_PHOTOS),
                filters,
                MAX_RECORDS_LIMIT,
                QueryBuilder.QUERY_KEYS,
                continuationMarker,
            );
 
            const receivedPositions = this.countReceivedPositions(page.records);
            if (receivedPositions === 0) {
                // No more records available, the count mismatch will be reported once all records are processed
                break;
            }
 
            records.push(...page.records);
            receivedRank += receivedPositions;
            if (page.continuationMarker && page.continuationMarker !== continuationMarker) {
                continuationMarker = page.continuationMarker;
            } else {
                continuationMarker = undefined;
                queryRank = receivedRank;
            }
        }
 
        return records;
    }
 
    /**
     * Counts the positions (photos) a page of picture records covers - every position has exactly one CPLAsset.
     * @remarks The number of CPLMasters is not reliable, since iCloud does not return a master for every asset
     * @param records - The records of the page, as returned by the backend
     * @returns The number of positions covered by the page
     */
    countReceivedPositions(records: any[]): number {
        return records.filter(record => record?.recordType === QueryBuilder.RECORD_TYPES.PHOTO_ASSET_RECORD).length;
    }
 
    /**
     * Filters unwanted picture records before post-processing
     * @param record - The record to be filtered
     * @param seen - An array of previously seen recordNames
     * @throws An iCPSError, in case the provided record should be ignored
     */
    filterPictureRecord(record: any, seen: Set<string>) {
        if (record?.deleted === true) {
            throw new iCPSError(ICLOUD_PHOTOS_ERR.DELETED_RECORD)
                .addContext(`record`, record);
        }
 
        if (!Resources.manager().syncHidden && record.fields?.isHidden?.value === 1) {
            throw new iCPSError(ICLOUD_PHOTOS_ERR.HIDDEN_RECORD)
                .addContext(`record`, record);
        }
 
        // If (Object.prototype.hasOwnProperty.call(seen, record.recordName)) {
        if (seen.has(record.recordName)) {
            throw new iCPSError(ICLOUD_PHOTOS_ERR.DUPLICATE_RECORD)
                .addContext(`record`, record);
        }
 
        if (record.recordType === QueryBuilder.RECORD_TYPES.CONTAINER_RELATION) {
            throw new iCPSError(ICLOUD_PHOTOS_ERR.UNWANTED_RECORD_TYPE)
                .addMessage(record.recordType)
                .addContext(`recordType`, record.recordType);
        }
 
        if (record.recordType !== QueryBuilder.RECORD_TYPES.PHOTO_MASTER_RECORD
            && record.recordType !== QueryBuilder.RECORD_TYPES.PHOTO_ASSET_RECORD) {
            throw new iCPSError(ICLOUD_PHOTOS_ERR.UNKNOWN_RECORD_TYPE)
                .addMessage(record.recordType)
                .addContext(`recordType`, record.recordType);
        }
    }
 
    /**
     * Fetching all pictures associated to an album within the given zone, identified by parentId
     * @param zone - Defines the zone to be used
     * @param parentId - The record name of the album, if undefined all pictures will be returned
     * @param hidden - If set and parentId is undefined, the hidden pictures will be returned instead of all pictures
     * @returns A tuple containing the plain records as returned by the backend and the expected number of assets within the album
     */
    async fetchAllPictureRecordsForZone(zone: QueryBuilder.Zones, parentId?: string, hidden: boolean = false): Promise<[any[], number]> {
        // Getting number of items in folder
        const expectedNumberOfRecords = await this.getPictureRecordsCountForZone(zone, parentId, hidden);
 
        // Creating requests, based on number of expected items
        const pictureRecordsRequests = this.buildPictureRecordsRequestsForZone(zone, expectedNumberOfRecords, parentId, hidden);
 
        // Merging arrays of arrays and waiting for all promises to settle - also if one of them fails, so no request is left running unobserved
        const allRecords: any[] = [];
        // Ranges might overlap, in case positions were re-requested
        const seen = new Set<string>();
 
        (await SyncEngineHelper.settleAll(pictureRecordsRequests)).forEach(records => {
            for (const record of records) {
                const recordKey = `${record?.recordType}/${record?.recordName}`;
                if (typeof record?.recordName === `string` && seen.has(recordKey)) {
                    continue;
                }
 
                seen.add(recordKey);
                allRecords.push(record);
            }
        });
 
        return [allRecords, expectedNumberOfRecords];
    }
 
    /**
     * Fetching all pictures associated to an album, identified by parentId
     * @param parentId - The record name of the album, if undefined all pictures will be returned (including hidden pictures, if enabled)
     * @returns An array of CPLMaster and CPLAsset records
     * @throws An iCPSError, in case the records could not be fetched
     * @emits iCPSEventRuntimeWarning.COUNT_MISMATCH - In case the number of fetched records does not match the expected number of records -  provides the album id, number of expected assets, actual CPL Assets and actual CPL Masters
     */
    async fetchAllCPLAssetsMasters(parentId?: string): Promise<[CPLAsset[], CPLMaster[]]> {
        Resources.logger(this).debug(`Fetching all picture records for album ${parentId === undefined ? `All photos` : parentId}`);
 
        let allRecords: any[];
        let expectedNumberOfRecords: number;
        try {
            [allRecords, expectedNumberOfRecords] = await this.fetchAllPictureRecordsForZone(QueryBuilder.Zones.Primary, parentId);
 
            // Merging assets of shared library, if available
            if (Resources.manager().sharedZoneAvailable && typeof parentId === `undefined`) { // Only fetch shared album records if no parentId is specified, since icloud api does not yet support shared records in albums
                Resources.logger(this).debug(`Fetching all picture records for album All photos for shared zone`);
                const [sharedRecords, sharedExpectedCount] = await this.fetchAllPictureRecordsForZone(QueryBuilder.Zones.Shared);
                allRecords = [...allRecords, ...sharedRecords];
                expectedNumberOfRecords += sharedExpectedCount;
            }
 
            // Merging hidden assets, if enabled, since they are not part of the 'All photos' query
            if (Resources.manager().syncHidden && typeof parentId === `undefined`) {
                const [hiddenRecords, hiddenExpectedCount] = await this.fetchAllHiddenPictureRecords();
                allRecords = [...allRecords, ...hiddenRecords];
                expectedNumberOfRecords += hiddenExpectedCount;
            }
        } catch (err) {
            throw new iCPSError(ICLOUD_PHOTOS_ERR.FETCH_RECORDS)
                .addMessage(`album ${parentId === undefined ? `'All photos'` : parentId}`)
                .addCause(err);
        }
 
        return this.parsePictureRecords(allRecords, expectedNumberOfRecords, parentId === undefined ? `All photos` : parentId);
    }
 
    /**
     * Fetching all hidden pictures
     * @returns An array of CPLMaster and CPLAsset records of all hidden pictures
     * @throws An iCPSError, in case the records could not be fetched
     * @emits iCPSEventRuntimeWarning.COUNT_MISMATCH - In case the number of fetched records does not match the expected number of records -  provides the album id, number of expected assets, actual CPL Assets and actual CPL Masters
     */
    async fetchHiddenCPLAssetsMasters(): Promise<[CPLAsset[], CPLMaster[]]> {
        Resources.logger(this).debug(`Fetching all hidden picture records`);
        try {
            const [hiddenRecords, hiddenExpectedCount] = await this.fetchAllHiddenPictureRecords();
            return this.parsePictureRecords(hiddenRecords, hiddenExpectedCount, HIDDEN_ALBUM_NAME);
        } catch (err) {
            throw new iCPSError(ICLOUD_PHOTOS_ERR.FETCH_RECORDS)
                .addMessage(`album '${HIDDEN_ALBUM_NAME}'`)
                .addCause(err);
        }
    }
 
    /**
     * Fetching the plain records of all hidden pictures of the primary and (if available) shared zone
     * @returns A tuple containing the plain records as returned by the backend and the expected number of hidden assets
     */
    async fetchAllHiddenPictureRecords(): Promise<[any[], number]> {
        let [hiddenRecords, expectedNumberOfRecords] = await this.fetchAllPictureRecordsForZone(QueryBuilder.Zones.Primary, undefined, true);
 
        if (Resources.manager().sharedZoneAvailable) {
            Resources.logger(this).debug(`Fetching all hidden picture records for shared zone`);
            const [sharedHiddenRecords, sharedExpectedCount] = await this.fetchAllPictureRecordsForZone(QueryBuilder.Zones.Shared, undefined, true);
            hiddenRecords = [...hiddenRecords, ...sharedHiddenRecords];
            expectedNumberOfRecords += sharedExpectedCount;
        }
 
        return [hiddenRecords, expectedNumberOfRecords];
    }
 
    /**
     * Filters and parses the plain records returned by the backend
     * @param allRecords - The plain records as returned by the backend
     * @param expectedNumberOfRecords - The number of assets expected within the records
     * @param albumName - The name of the album (used for logging)
     * @returns An array of CPLMaster and CPLAsset records
     * @emits iCPSEventRuntimeWarning.COUNT_MISMATCH - In case the number of fetched records does not match the expected number of records -  provides the album id, number of expected assets, actual CPL Assets and actual CPL Masters
     */
    parsePictureRecords(allRecords: any[], expectedNumberOfRecords: number, albumName: string): [CPLAsset[], CPLMaster[]] {
        let cplMasters: CPLMaster[] = [];
        const cplAssets: CPLAsset[] = [];
 
        // Post-processing response
        const seen = new Set<string>();
        const ignoredAssets: iCPSError[] = [];
        const hiddenAssets = new Set<string>();
        const hiddenMasters = new Set<string>();
        for (const record of allRecords) {
            try {
                this.filterPictureRecord(record, seen);
 
                if (record.recordType === QueryBuilder.RECORD_TYPES.PHOTO_MASTER_RECORD) {
                    cplMasters.push(CPLMaster.parseFromQuery(record));
                    seen.add(record.recordName);
                }
 
                if (record.recordType === QueryBuilder.RECORD_TYPES.PHOTO_ASSET_RECORD) {
                    cplAssets.push(CPLAsset.parseFromQuery(record));
                    seen.add(record.recordName);
                }
            } catch (err) {
                if ((err as iCPSError).code === ICLOUD_PHOTOS_ERR.HIDDEN_RECORD.code) {
                    hiddenAssets.add(record.recordName);
                    if (record.fields?.masterRef?.value?.recordName) {
                        hiddenMasters.add(record.fields.masterRef.value.recordName);
                    }
                }
 
                // Summarizing errors/warnings
                ignoredAssets.push((err as iCPSError));
            }
        }
 
        // Ignored hidden assets are still part of an album's count and their masters are returned as well - removing both (unless the master is still referenced)
        if (hiddenAssets.size > 0) {
            const referencedMasters = new Set(cplAssets.map(asset => asset.masterRef));
            cplMasters = cplMasters.filter(master => !hiddenMasters.has(master.recordName) || referencedMasters.has(master.recordName));
            expectedNumberOfRecords -= hiddenAssets.size;
        }
 
        // Pretty printing ignored assets
        if (ignoredAssets.length > 0) {
            Resources.logger(this).info(`Ignoring ${ignoredAssets.length} assets for ${albumName}:`);
            const erroredAssets = ignoredAssets.filter(err => err.code !== ICLOUD_PHOTOS_ERR.UNWANTED_RECORD_TYPE.code && err.code !== ICLOUD_PHOTOS_ERR.HIDDEN_RECORD.code); // Filtering 'expected' errors
            if (erroredAssets.length > 0) {
                Resources.logger(this).warn(`${erroredAssets.length} unexpected errors for ${albumName}: ${erroredAssets.map(err => err.code).join(`, `)}`);
            }
        }
 
        // There should be one CPLMaster and one CPLAsset per record, however the iCloud response is sometimes not adhering to this.
        if (cplMasters.length !== expectedNumberOfRecords || cplAssets.length !== expectedNumberOfRecords) {
            Resources.emit(iCPSEventRuntimeWarning.COUNT_MISMATCH,
                albumName,
                expectedNumberOfRecords,
                cplAssets.length,
                cplMasters.length,
            );
        } else {
            Resources.logger(this).debug(`Received expected amount (${expectedNumberOfRecords}) of records for album ${albumName}`);
        }
 
        return [cplAssets, cplMasters];
    }
 
    /**
     * Downloads an asset to the correct file location and applies relevant metadata to the file
     * @param asset - The asset to be downloaded
     * @returns A promise, that resolves, once the asset has been written to disk
     * @throws An error, in case the asset could not be downloaded
     */
    async downloadAsset(asset: Asset): Promise<void> {
        if (!asset.downloadURL) {
            throw new iCPSError(ICLOUD_PHOTOS_ERR.MISSING_DOWNLOAD_URL)
                .addContext(`asset`, asset);
        }
 
        await Resources.network().downloadData(asset.downloadURL, asset.getAssetFilePath(), asset.modified);
    }
 
    /**
     * Deletes the records in the remote library
     * @remarks Since the shared library currently does not support it's own directory tree / WebUI does not show pictures in folders we only do this for the primary zone, because archiving is only possible of folders
     * @param recordNames - A list of record names that need to be deleted
     * @returns A Promise, that fulfils once the operation has been performed
     * @throws An iCPSError, in case the records could not be deleted
     */
    async deleteAssets(recordNames: string[]) {
        Resources.logger(this).debug(`Deleting ${recordNames.length} assets: ${jsonc.stringify(recordNames)}`);
        await this.performOperation(QueryBuilder.Zones.Primary, `update`, QueryBuilder.getIsDeletedField(), recordNames);
    }
}