Skip to content

Commit

Permalink
fix: Moving network limiter into NetworkManager for consistent handling
Browse files Browse the repository at this point in the history
  • Loading branch information
steilerDev committed Aug 11, 2023
1 parent cff79e5 commit 8cf6787
Show file tree
Hide file tree
Showing 12 changed files with 355 additions and 282 deletions.
4 changes: 2 additions & 2 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
"env": {
"NODE_NO_WARNINGS": "1"
},
"envFile": "${workspaceFolder}/.vscode/test.env"
"envFile": "${workspaceFolder}/.vscode/private.env"
}, {
"name": "Run API Tests",
"type": "node",
Expand Down Expand Up @@ -66,7 +66,7 @@
"--runInBand",
"--config", "jest.config.json",
//"--detectOpenHandles",
"test/unit/sync-engine.test.ts"
"test/unit/icloud.test.ts"
],
"env": {
"NODE_NO_WARNINGS": "1"
Expand Down
4 changes: 4 additions & 0 deletions app/knip.config.jsonc
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,9 @@
"ts-json-schema-generator", // Loaded by 'build-schema' script
"typedoc-plugin-markdown", // Loaded by 'typedoc' script
"typedoc-github-wiki-theme" // Loaded by 'typedoc' script
],
"ignoreBinaries": [ // Not sure why they are reported as unused
"eslint.config.json",
"package.json"
]
}
238 changes: 143 additions & 95 deletions app/package-lock.json

Large diffs are not rendered by default.

3 changes: 2 additions & 1 deletion app/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@
"test": "NODE_NO_WARNINGS=1 NODE_OPTIONS='--experimental-vm-modules' npx jest --config jest.config.json test/unit/",
"test:all": "NODE_NO_WARNINGS=1 NODE_OPTIONS='--experimental-vm-modules' npx jest --config jest.config.json",
"test:api": "NODE_NO_WARNINGS=1 NODE_OPTIONS='--experimental-vm-modules' npx jest --config jest.config.json test/api/",
"execute": "node bin/main.js"
"execute": "node bin/main.js",
"execute:debug": "node --inspect=0.0.0.0:9229 bin/main.js daemon"
},
"repository": {
"type": "git",
Expand Down
1 change: 1 addition & 0 deletions app/src/app/event/error-handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,7 @@ export class ErrorHandler {
truncatedData.push(`${data[i]}\n`);
}
}

truncatedData.push(null);

await this.compressStream(targetPath, truncatedData);
Expand Down
16 changes: 2 additions & 14 deletions app/src/lib/icloud/icloud-photos/icloud-photos.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import {Asset} from '../../photos-library/model/asset.js';
import {CPLAlbum, CPLAsset, CPLMaster} from './query-parser.js';
import {iCPSError} from '../../../app/error/error.js';
import {ICLOUD_PHOTOS_ERR} from '../../../app/error/error-codes.js';
import PQueue from 'p-queue';
import {ResourceManager} from '../../resource-manager/resource-manager.js';
import {ENDPOINTS} from '../../resource-manager/network.js';
import {SyncEngineHelper} from '../../sync-engine/helper.js';
Expand All @@ -32,21 +31,10 @@ export class iCloudPhotos {
*/
ready: Promise<void>;

/**
* The queue holding all query operations. Used to rate limit metadata fetching
*/
queryQueue: PQueue;

/**
* Creates a new iCloud Photos Class
* @param auth - The populated authentication object
*/
constructor() {
this.queryQueue = new PQueue({
intervalCap: ResourceManager.metadataRate[0],
interval: ResourceManager.metadataRate[1],
});

ResourceManager.events(this).on(iCPSEventPhotos.SETUP_COMPLETED, async () => {
await this.checkingIndexingStatus();
});
Expand Down Expand Up @@ -182,7 +170,7 @@ export class iCloudPhotos {
data.resultsLimit = resultsLimit;
}

const queryResponse = await this.queryQueue.add(async () => ResourceManager.network.post(ENDPOINTS.PHOTOS.PATH.QUERY, data, config)) as AxiosResponse<any, any>;
const queryResponse = await ResourceManager.network.post(ENDPOINTS.PHOTOS.PATH.QUERY, data, config);

const fetchedRecords = queryResponse?.data?.records;
if (!fetchedRecords || !Array.isArray(fetchedRecords)) {
Expand Down Expand Up @@ -224,7 +212,7 @@ export class iCloudPhotos {
},
}));

const operationResponse = await this.queryQueue.add(async () => ResourceManager.network.post(ENDPOINTS.PHOTOS.PATH.MODIFY, data, config)) as AxiosResponse<any, any>;
const operationResponse = await ResourceManager.network.post(ENDPOINTS.PHOTOS.PATH.MODIFY, data, config);
const fetchedRecords = operationResponse?.data?.records;
if (!fetchedRecords || !Array.isArray(fetchedRecords)) {
throw new iCPSError(ICLOUD_PHOTOS_ERR.UNEXPECTED_OPERATIONS_RESPONSE)
Expand Down
6 changes: 1 addition & 5 deletions app/src/lib/icloud/icloud.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import {MFAMethod} from './mfa/mfa-method.js';
import {iCPSError} from '../../app/error/error.js';
import {ICLOUD_PHOTOS_ERR, MFA_ERR, AUTH_ERR} from '../../app/error/error-codes.js';
import {ResourceManager} from '../resource-manager/resource-manager.js';
import {ENDPOINTS, HEADER} from '../resource-manager/network.js';
import {ENDPOINTS} from '../resource-manager/network.js';
import {iCPSEventCloud, iCPSEventError, iCPSEventMFA, iCPSEventPhotos} from '../resource-manager/events.js';

/**
Expand Down Expand Up @@ -90,7 +90,6 @@ export class iCloud {
const url = ENDPOINTS.AUTH.BASE + ENDPOINTS.AUTH.PATH.SIGNIN;

const config: AxiosRequestConfig = {
headers: HEADER.AUTH,
params: {
isRememberMeEnabled: true,
},
Expand Down Expand Up @@ -168,7 +167,6 @@ export class iCloud {

const url = method.getResendURL();
const config: AxiosRequestConfig = {
headers: HEADER.AUTH,
validateStatus: method.resendSuccessful.bind(method),
};
const data = method.getResendPayload();
Expand Down Expand Up @@ -204,7 +202,6 @@ export class iCloud {

const url = method.getEnterURL();
const config: AxiosRequestConfig = {
headers: HEADER.AUTH,
validateStatus: method.enterSuccessful.bind(method),
};
const data = method.getEnterPayload(mfa);
Expand All @@ -228,7 +225,6 @@ export class iCloud {

const url = ENDPOINTS.AUTH.BASE + ENDPOINTS.AUTH.PATH.TRUST;
const config: AxiosRequestConfig = {
headers: HEADER.AUTH,
validateStatus: status => status === 204,
};

Expand Down

0 comments on commit 8cf6787

Please sign in to comment.