Wrap JSON.parse in try/catch (#8363)

* fix(telemetry): wrap JSON.parse in try/catch

* fix: always write the store

* chore(lint): fix
This commit is contained in:
Nate Moore 2023-09-01 14:19:12 -05:00 committed by GitHub
parent 7a91600a9e
commit 0ce0720c7f
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 12 additions and 5 deletions

View file

@ -0,0 +1,5 @@
---
'@astrojs/telemetry': patch
---
Wrap `JSON.parse` in `try`/`catch`

View file

@ -46,13 +46,15 @@ export class GlobalConfig {
if (this._store) return this._store;
this.ensureDir();
if (fs.existsSync(this.file)) {
this._store = JSON.parse(fs.readFileSync(this.file).toString());
} else {
const store = {};
this._store = store;
try {
this._store = JSON.parse(fs.readFileSync(this.file).toString());
} catch {}
}
if (!this._store) {
this._store = {};
this.write();
}
return this._store!;
return this._store;
}
private set store(value: Record<string, any>) {
this._store = value;