Skip to content

Commit

Permalink
fix: HAR file properly tracking header values and query parameter val…
Browse files Browse the repository at this point in the history
…ues are now strings
  • Loading branch information
steilerDev committed Oct 3, 2023
1 parent 0228fcf commit bcea4ae
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 24 deletions.
2 changes: 1 addition & 1 deletion .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
],
"preLaunchTask": "Build App",
"program": "${workspaceFolder}/app/src/main.ts",
"args": ["token"],
"args": ["sync"],
"outFiles": [
"${workspaceFolder}/app/build/out/**/*.js",
],
Expand Down
2 changes: 1 addition & 1 deletion app/src/lib/icloud/icloud.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ export class iCloud {

const config: AxiosRequestConfig = {
params: {
isRememberMeEnabled: true,
isRememberMeEnabled: `true`,
},
// 409 is expected, if MFA is required - 200 is expected, if authentication succeeds immediately
validateStatus: status => status === 409 || status === 200,
Expand Down
24 changes: 13 additions & 11 deletions app/src/lib/resources/network-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import {Cookie} from "tough-cookie";
import {iCPSError} from "../../app/error/error.js";
import {RESOURCES_ERR} from "../../app/error/error-codes.js";
import {AxiosHarTracker} from "axios-har-tracker";
import {FILE_ENCODING} from "./resource-types.js";
import PQueue from "p-queue";
import {Resources} from "./main.js";
import {iCPSAppOptions} from "../../app/factory.js";
Expand Down Expand Up @@ -218,6 +217,12 @@ export class NetworkManager {
Origin: `https://www.${this.iCloudRegionUrl(resources.region)}`,
},
});

if (resources.enableNetworkCapture) {
this._harTracker = new AxiosHarTracker(this._axios as any);
this.resetHarTracker(this._harTracker);
}

this._headerJar = new HeaderJar(this._axios);

this._streamingCCYLimiter = new PQueue({
Expand All @@ -228,11 +233,6 @@ export class NetworkManager {
this._streamingAxios = axios.create({
responseType: `stream`,
});

if (resources.enableNetworkCapture) {
this._harTracker = new AxiosHarTracker(this._axios as any);
this.resetHarTracker(this._harTracker);
}
}

/**
Expand Down Expand Up @@ -330,7 +330,7 @@ export class NetworkManager {

Resources.logger(this).info(`Generated HAR archive with ${generatedObject.log.entries.length} entries`);

await fs.writeFile(Resources.manager().harFilePath, jsonc.stringify(generatedObject), {encoding: FILE_ENCODING, flag: `w`});
jsonc.write(Resources.manager().harFilePath, generatedObject, {autoPath: true});
Resources.logger(this).info(`HAR file written`);
return true;
} catch (err) {
Expand Down Expand Up @@ -509,19 +509,21 @@ export class NetworkManager {
const file = path.parse(location);
const duplicateFolder = path.join(Resources.manager().dataDir, `duplicates`);
const duplicateFolderExists = await fs.stat(duplicateFolder)
.then(stat => stat.isDirectory())
.then(() => true)
.catch(() => false);

if (!duplicateFolderExists) {
await fs.mkdir(duplicateFolder);
await fs.mkdir(duplicateFolder)
.catch;
}

const origFileName = file.name + `-orig` + file.ext;
const origFileLinked = await fs.lstat(path.join(duplicateFolder, origFileName))
.then(stat => stat.isSymbolicLink())
.then(() => true)
.catch(() => false);
if (!origFileLinked) {
await fs.link(location, path.join(duplicateFolder, origFileName));
await fs.link(location, path.join(duplicateFolder, origFileName))
.catch();
}

const duplicateFileName = file.name + `-dup-` + randomInt(0, 1000000).toString() + file.ext;
Expand Down
6 changes: 3 additions & 3 deletions app/src/lib/sync-engine/helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -144,19 +144,19 @@ function getProcessingQueues<T>(remoteEntities: PEntity<T>[], _localEntities: PL
const localEntity = localEntities[remoteEntity.getUUID()];
if (!localEntity || !remoteEntity.equal(localEntity)) {
// No local entity OR local entity does not match remote entity -> Remote asset will be added & local asset will not be removed from deletion queue
// this.logger.debug(`Adding new remote entity ${remoteEntity.getDisplayName()}`);
Resources.logger(`SyncHelper`).debug(`Adding new remote entity ${remoteEntity.getDisplayName()}`);
// Making sure entities have all relevant properties
toBeAdded.push(remoteEntity.apply(localEntity));
} else {
// Local asset matches remote asset, nothing to do, but preventing local asset to be deleted
// this.logger.debug(`Keeping existing local entity ${remoteEntity.getDisplayName()}`);
Resources.logger(`SyncHelper`).debug(`Keeping existing local entity ${remoteEntity.getDisplayName()}`);
toBeKept.push(remoteEntity.apply(localEntity));
delete localEntities[remoteEntity.getUUID()];
}
});
// The original library should only hold those records, that have not been referenced by the remote state, removing them
const toBeDeleted = Object.values(localEntities);
// This.logger.debug(`Adding ${toBeAdded.length} remote entities, removing ${toBeDeleted.length} local entities, keeping ${toBeKept.length} local entities`);
Resources.logger(`SyncHelper`).debug(`Got ${toBeDeleted.length} remaining local entities that need to be deleted: ${toBeDeleted.map(entity => (entity as any).getDisplayName()).join(`, `)}`);
return [toBeDeleted, toBeAdded, toBeKept];
}

Expand Down
10 changes: 2 additions & 8 deletions app/src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,9 @@ import {CLIInterface} from "./app/event/cli.js";
import {appFactory} from "./app/factory.js";
import {MetricsExporter} from "./app/event/metrics-exporter.js";
import {LogInterface} from "./app/event/log.js";
import {iCPSApp} from "./app/icloud-app.js";

let app: iCPSApp;
try {
app = await appFactory(process.argv);
} catch (_err) {
// AppFactory will print appropriate error messages
process.exit(3);
}
const app = await appFactory(process.argv)
.catch(() => process.exit(3)); // Error message is printed by factory

const _errorHandler = new ErrorHandler();
const _logInterface = new LogInterface();
Expand Down

0 comments on commit bcea4ae

Please sign in to comment.