Skip to content

Commit

Permalink
Fixed exception thrown while machine id generation.
Browse files Browse the repository at this point in the history
Machine ID generation was throwing an exception which was looping and causing `Maximum call stack size exceeded`
  • Loading branch information
ganeshrvel committed Jun 14, 2024
1 parent 47ca4c3 commit b32b57b
Show file tree
Hide file tree
Showing 9 changed files with 57 additions and 25 deletions.
27 changes: 22 additions & 5 deletions app/classes/Storage.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@ import { checkIf } from '../utils/checkIf';
import { isEmpty } from '../utils/funcs';

export default class Storage {
constructor(filePath) {
constructor(filePath, doNotLog = false) {
this.filePath = filePath;
this.doNotLog = doNotLog;
}

getAll() {
Expand All @@ -23,7 +24,11 @@ export default class Storage {

return JSON.parse(_stream);
} catch (e) {
log.error(e, `Storage -> getAll`);
if (this.doNotLog) {
console.error(e, `Storage -> getAll`);
} else {
log.error(e, `Storage -> getAll`);
}
}
}

Expand Down Expand Up @@ -54,15 +59,23 @@ export default class Storage {

return _return;
} catch (e) {
log.error(e, `Storage -> getItems`);
if (this.doNotLog) {
console.error(e, `Storage -> getItems`);
} else {
log.error(e, `Storage -> getItems`);
}
}
}

setAll({ ...data }) {
try {
writeFileSync(this.filePath, JSON.stringify({ ...data }));
} catch (e) {
log.error(e, `Storage -> setAll`);
if (this.doNotLog) {
console.error(e, `Storage -> setAll`);
} else {
log.error(e, `Storage -> setAll`);
}
}
}

Expand All @@ -75,7 +88,11 @@ export default class Storage {
JSON.stringify({ ...currentSettings, ...data })
);
} catch (e) {
log.error(e, `Storage -> setAll`);
if (this.doNotLog) {
console.error(e, `Storage -> setItems`);
} else {
log.error(e, `Storage -> setItems`);
}
}
}
}
2 changes: 2 additions & 0 deletions app/constants/paths.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ const logFileName = IS_DEV
const logDir = join(profileDir, `./logs`);
const logFile = join(logDir, `./${APP_NAME}-${logFileName}`);
const settingsFile = join(profileDir, `./settings.json`);
const identifierFile = join(profileDir, `./identifier.json`);
const devAppUpdateFile = join(configDir, `./dev-app-update.yml`);

export const PATHS = {
Expand All @@ -41,6 +42,7 @@ export const PATHS = {
logDir: resolve(logDir),
logFile: resolve(logFile),
settingsFile: resolve(settingsFile),
identifierFile: resolve(identifierFile),
devAppUpdateFile: resolve(devAppUpdateFile),
prevProfileDir: resolve(prevProfileDir),
desktopDir: join(homeDir, `/Desktop`),
Expand Down
30 changes: 18 additions & 12 deletions app/helpers/identifiers.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,24 @@
import { machineId } from 'node-machine-id';
import { settingsStorage } from './storageHelper';
import { v6 as uuidv6 } from 'uuid';

export async function getMachineId() {
const settings = settingsStorage.getItems(['machineId']);
import { identifierStorage } from './storageHelper';
import { isEmpty } from '../utils/funcs';

if (!settings?.machineId) {
const _machineId = await machineId();
export function getMachineId() {
try {
const settings = identifierStorage.getItems(['machineId']);

settingsStorage.setItems({
machineId: _machineId,
});
if (!settings?.machineId || isEmpty(settings?.machineId)) {
const _machineId = uuidv6();

return _machineId;
}
identifierStorage.setItems({
machineId: _machineId,
});

return _machineId;
}

return settings.machineId;
return settings.machineId;
} catch (e) {
console.error(`Unable to find machineId. Error: ${e}`);
}
}
6 changes: 4 additions & 2 deletions app/helpers/storageHelper.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { PATHS } from '../constants/paths';
import Storage from '../classes/Storage';

const { settingsFile } = PATHS;
const { settingsFile, identifierFile } = PATHS;

export const settingsStorage = new Storage(settingsFile);
export const identifierStorage = new Storage(identifierFile, true);

export const settingsStorage = new Storage(settingsFile, false);
2 changes: 1 addition & 1 deletion app/services/analytics/mixpanelAnalytics.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ export class MixpanelAnalytics {
}

// this is a hashed value (sha-256)
this.machineId = await getMachineId();
this.machineId = getMachineId();

if (ENV_FLAVOR.enableMixpanelAnalytics) {
mixpanel.init(SERVICE_KEYS.mixpanelAnalytics);
Expand Down
2 changes: 1 addition & 1 deletion app/services/sentry/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ class SentryService {
release: pkginfo.version,
});

this.machineId = await getMachineId();
this.machineId = getMachineId();
}

async report({ error, title, mtpMode }) {
Expand Down
2 changes: 1 addition & 1 deletion app/utils/log.js
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ export const log = {
let _deviceInfoStrigified = '';
const deviceInfo = getDeviceInfo();
const mtpMode = getMtpModeSetting();
const uuid = await getMachineId();
const uuid = getMachineId();

if (!isEmpty(deviceInfo)) {
Object.keys(deviceInfo).forEach((a) => {
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "openmtp",
"productName": "OpenMTP",
"version": "3.2.23",
"version": "3.2.24",
"description": "OpenMTP | Android File Transfer for macOS",
"scripts": {
"build": "yarn lint && concurrently \"yarn build-main\" \"yarn build-renderer\"",
Expand Down Expand Up @@ -101,7 +101,6 @@
"mkdirp": "^1.0.4",
"nice-utils": "^1.0.3",
"node-mac-permissions": "^2.2.1",
"node-machine-id": "^1.1.12",
"normalize.css": "^8.0.1",
"os": "^0.1.2",
"path": "^0.12.7",
Expand All @@ -126,6 +125,7 @@
"url": "^0.11.0",
"usb-detection": "^4.14.1",
"util": "^0.12.4",
"uuid": "^10.0.0",
"zlib": "^1.0.5"
},
"devDependencies": {
Expand Down
7 changes: 6 additions & 1 deletion yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -8877,7 +8877,7 @@ node-mac-permissions@^2.2.1:
bindings "^1.5.0"
node-addon-api "^3.0.2"

node-machine-id@^1.1.12, node-machine-id@^1.1.9:
node-machine-id@^1.1.9:
version "1.1.12"
resolved "https://registry.yarnpkg.com/node-machine-id/-/node-machine-id-1.1.12.tgz#37904eee1e59b320bb9c5d6c0a59f3b469cb6267"
integrity sha512-QNABxbrPa3qEIfrE6GOJ7BYIuignnJw7iQ2YPbc3Nla1HzRJjXzZOiikfF8m7eAMfichLt3M4VgLOetqgDmgGQ==
Expand Down Expand Up @@ -13167,6 +13167,11 @@ utils-merge@1.0.1:
resolved "https://registry.yarnpkg.com/utils-merge/-/utils-merge-1.0.1.tgz#9f95710f50a267947b2ccc124741c1028427e713"
integrity sha1-n5VxD1CiZ5R7LMwSR0HBAoQn5xM=

uuid@^10.0.0:
version "10.0.0"
resolved "https://registry.yarnpkg.com/uuid/-/uuid-10.0.0.tgz#5a95aa454e6e002725c79055fd42aaba30ca6294"
integrity sha512-8XkAphELsDnEGrDxUOHB3RGvXz6TeuYSGEZBOjtTtPm2lwhGBjLgOzLHB63IUWfBpNucQjND6d3AOudO+H3RWQ==

uuid@^3.0.1, uuid@^3.3.2:
version "3.4.0"
resolved "https://registry.yarnpkg.com/uuid/-/uuid-3.4.0.tgz#b23e4358afa8a202fe7a100af1f5f883f02007ee"
Expand Down

0 comments on commit b32b57b

Please sign in to comment.