Skip to content

Commit

Permalink
fix: Swap JSON for jsonc to avoid circular errors
Browse files Browse the repository at this point in the history
Fixes #327
  • Loading branch information
Frank Steiler committed Sep 17, 2023
1 parent 62b0a05 commit 3be72cd
Show file tree
Hide file tree
Showing 10 changed files with 28 additions and 28 deletions.
20 changes: 5 additions & 15 deletions app/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions app/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@
"cli-progress": "^3.11.2",
"commander": "^10.0.0",
"croner": "^6.0.3",
"jsonc": "^2.0.0",
"p-event": "^5.0.1",
"p-queue": "^7.2.0",
"p-timeout": "^6.1.2",
Expand Down
3 changes: 2 additions & 1 deletion app/src/app/event/error-handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {Readable} from 'stream';
import {pEvent} from 'p-event';
import bt from '@backtrace-labs/node';
import {MFAMethod} from '../../lib/icloud/mfa/mfa-method.js';
import { jsonc } from "jsonc";

/**
* List of errors that will never get reported
Expand Down Expand Up @@ -87,7 +88,7 @@ export class ErrorHandler {
beforeSend(data: bt.BacktraceData) {
return Object.assign(
data,
JSON.parse(ErrorHandler.maskConfidentialData(JSON.stringify(data))),
jsonc.parse(ErrorHandler.maskConfidentialData(jsonc.stringify(data))),
);
},
});
Expand Down
5 changes: 3 additions & 2 deletions app/src/lib/icloud/icloud-photos/icloud-photos.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {ENDPOINTS} from '../../resources/network-types.js';
import {SyncEngineHelper} from '../../sync-engine/helper.js';
import {iCPSEventPhotos, iCPSEventRuntimeWarning} from '../../resources/events-types.js';
import fs from 'fs/promises';
import { jsonc } from 'jsonc';

/**
* To perform an operation, a record change tag is required. Hardcoding it for now
Expand Down Expand Up @@ -339,7 +340,7 @@ export class iCloudPhotos {
cplAlbums.push(CPLAlbum.parseFromQuery(album));
}
} catch (err) {
Resources.logger(this).info(`Error processing CPLAlbum: ${JSON.stringify(album)}: ${err.message}`);
Resources.logger(this).info(`Error processing CPLAlbum: ${jsonc.stringify(album)}: ${err.message}`);
}
}

Expand Down Expand Up @@ -576,7 +577,7 @@ export class iCloudPhotos {
* @throws An iCPSError, in case the records could not be deleted
*/
async deleteAssets(recordNames: string[]) {
Resources.logger(this).debug(`Deleting ${recordNames.length} assets: ${JSON.stringify(recordNames)}`);
Resources.logger(this).debug(`Deleting ${recordNames.length} assets: ${jsonc.stringify(recordNames)}`);
await this.performOperation(QueryBuilder.Zones.Primary, `update`, QueryBuilder.getIsDeletedField(), recordNames);
}
}
5 changes: 3 additions & 2 deletions app/src/lib/icloud/icloud.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {Resources} from '../resources/main.js';
import {ENDPOINTS} from '../resources/network-types.js';
import {iCPSEventCloud, iCPSEventMFA, iCPSEventPhotos, iCPSEventRuntimeWarning} from '../resources/events-types.js';
import pTimeout from 'p-timeout';
import { jsonc } from 'jsonc';

/**
* This class holds the iCloud connection
Expand Down Expand Up @@ -178,7 +179,7 @@ export class iCloud {
};
const data = method.getResendPayload();

Resources.logger(this).debug(`Requesting MFA code via URL ${url} with data ${JSON.stringify(data)}`);
Resources.logger(this).debug(`Requesting MFA code via URL ${url} with data ${jsonc.stringify(data)}`);

try {
const response = await Resources.network().put(url, data, config);
Expand Down Expand Up @@ -215,7 +216,7 @@ export class iCloud {
};
const data = method.getEnterPayload(mfa);

Resources.logger(this).debug(`Entering MFA code via URL ${url} with data ${JSON.stringify(data)}`);
Resources.logger(this).debug(`Entering MFA code via URL ${url} with data ${jsonc.stringify(data)}`);
await Resources.network().post(url, data, config);

Resources.logger(this).info(`MFA code correct!`);
Expand Down
7 changes: 4 additions & 3 deletions app/src/lib/icloud/mfa/mfa-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {iCPSError} from '../../../app/error/error.js';
import {MFA_ERR} from '../../../app/error/error-codes.js';
import {Resources} from '../../resources/main.js';
import {iCPSEventMFA, iCPSEventRuntimeWarning} from '../../resources/events-types.js';
import { jsonc } from 'jsonc';

/**
* The MFA timeout value in milliseconds
Expand Down Expand Up @@ -70,7 +71,7 @@ export class MFAServer {
/* c8 ignore start */
// Never starting the server just to see logger message
Resources.emit(iCPSEventMFA.STARTED, Resources.manager().mfaServerPort);
Resources.logger(this).info(`Exposing endpoints: ${JSON.stringify(Object.values(MFA_SERVER_ENDPOINTS))}`);
Resources.logger(this).info(`Exposing endpoints: ${jsonc.stringify(Object.values(MFA_SERVER_ENDPOINTS))}`);
/* c8 ignore stop */
});

Expand Down Expand Up @@ -112,7 +113,7 @@ export class MFAServer {
Resources.emit(iCPSEventRuntimeWarning.MFA_ERROR, new iCPSError(MFA_ERR.ROUTE_NOT_FOUND)
.addMessage(req.url)
.addContext(`request`, req));
this.sendResponse(res, 404, `Route not found, available endpoints: ${JSON.stringify(Object.values(MFA_SERVER_ENDPOINTS))}`);
this.sendResponse(res, 404, `Route not found, available endpoints: ${jsonc.stringify(Object.values(MFA_SERVER_ENDPOINTS))}`);
}
}

Expand Down Expand Up @@ -177,7 +178,7 @@ export class MFAServer {
*/
sendResponse(res: http.ServerResponse, code: number, msg: string) {
res.writeHead(code, {"Content-Type": `application/json`});
res.end(JSON.stringify({message: msg}));
res.end(jsonc.stringify({message: msg}));
}

/**
Expand Down
3 changes: 2 additions & 1 deletion app/src/lib/photos-library/model/album.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { jsonc } from "jsonc";
import {LIBRARY_ERR} from "../../../app/error/error-codes.js";
import {iCPSError} from "../../../app/error/error.js";
import {CPLAlbum} from "../../icloud/icloud-photos/query-parser.js";
Expand Down Expand Up @@ -145,7 +146,7 @@ export class Album implements PEntity<Album> {
// Assets might be undefined
const thisAssets = this.assets ? this.assets : {};
const otherAssets = assets ? assets : {};
return JSON.stringify(Object.keys(thisAssets).sort()) === JSON.stringify(Object.keys(otherAssets).sort());
return jsonc.stringify(Object.keys(thisAssets).sort()) === jsonc.stringify(Object.keys(otherAssets).sort());
}

/**
Expand Down
3 changes: 2 additions & 1 deletion app/src/lib/resources/network-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import PQueue from "p-queue";
import {Resources} from "./main.js";
import {iCPSAppOptions} from "../../app/factory.js";
import {pEvent} from "p-event";
import { jsonc } from "jsonc";

/**
* Object holding all necessary information for a specific header value, that needs to be reused across multiple requests
Expand Down Expand Up @@ -327,7 +328,7 @@ export class NetworkManager {

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

await fs.writeFile(Resources.manager().harFilePath, JSON.stringify(generatedObject), {encoding: FILE_ENCODING, flag: `w`});
await fs.writeFile(Resources.manager().harFilePath, jsonc.stringify(generatedObject), {encoding: FILE_ENCODING, flag: `w`});
Resources.logger(this).info(`HAR file written`);
return true;
} catch (err) {
Expand Down
4 changes: 3 additions & 1 deletion app/src/lib/resources/network-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
* This file holds information relevant to networking, as well as type definitions of the expected responses
*/

import { jsonc } from "jsonc";

/**
* Hard coded client id, extracted from previous requests
*/
Expand All @@ -15,7 +17,7 @@ export const USER_AGENT = `Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:97.0
/**
* Client information shared with the iCloud backend based on the user agent
*/
export const CLIENT_INFO = JSON.stringify({
export const CLIENT_INFO = jsonc.stringify({
U: USER_AGENT,
L: `en-US`,
Z: `GMT+01:00`,
Expand Down
5 changes: 3 additions & 2 deletions app/src/lib/resources/resource-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {FILE_ENCODING, HAR_FILE_NAME, LOG_FILE_NAME, METRICS_FILE_NAME, PhotosAc
import {LogLevel} from "../../app/event/log.js";
import {Resources} from "./main.js";
import {iCPSEventRuntimeWarning} from "./events-types.js";
import { jsonc } from "jsonc";

/**
* This class handles access to the .icloud-photos-sync resource file and handles currently applied configurations from the CLI and environment variables
Expand Down Expand Up @@ -42,7 +43,7 @@ export class ResourceManager {
_readResourceFile(): ResourceFile {
try {
Resources.logger(this).debug(`Reading resource file from ${this.resourceFilePath}`);
const resourceFileData = JSON.parse(readFileSync(this.resourceFilePath, {encoding: FILE_ENCODING}));
const resourceFileData = jsonc.parse(readFileSync(this.resourceFilePath, {encoding: FILE_ENCODING}));
return Resources.validator().validateResourceFile(resourceFileData);
} catch (err) {
Resources.emit(iCPSEventRuntimeWarning.RESOURCE_FILE_ERROR,
Expand All @@ -63,7 +64,7 @@ export class ResourceManager {
libraryVersion: this._resources.libraryVersion,
trustToken: this._resources.trustToken,
};
const resourceFileData = JSON.stringify(formattedResourceFile, null, 4);
const resourceFileData = jsonc.stringify(formattedResourceFile, null, 4);
Resources.logger(this).debug(`Writing resource file to ${this.resourceFilePath}`);

writeFileSync(this.resourceFilePath, resourceFileData, {encoding: FILE_ENCODING});
Expand Down

0 comments on commit 3be72cd

Please sign in to comment.