diff options
author | 2023-09-01 14:19:12 -0500 | |
---|---|---|
committer | 2023-09-01 14:19:12 -0500 | |
commit | 0ce0720c7f2c7ba21dddfea0b75d1e9b39c6a274 (patch) | |
tree | 3c2d4e2f0fb2467a1f439c8700889a6fe87df906 | |
parent | 7a91600a9e9d26f214890f958a31339edd82311c (diff) | |
download | astro-0ce0720c7f2c7ba21dddfea0b75d1e9b39c6a274.tar.gz astro-0ce0720c7f2c7ba21dddfea0b75d1e9b39c6a274.tar.zst astro-0ce0720c7f2c7ba21dddfea0b75d1e9b39c6a274.zip |
Wrap `JSON.parse` in `try`/`catch` (#8363)
* fix(telemetry): wrap JSON.parse in try/catch
* fix: always write the store
* chore(lint): fix
-rw-r--r-- | .changeset/fair-berries-study.md | 5 | ||||
-rw-r--r-- | packages/telemetry/src/config.ts | 12 |
2 files changed, 12 insertions, 5 deletions
diff --git a/.changeset/fair-berries-study.md b/.changeset/fair-berries-study.md new file mode 100644 index 000000000..fadde388e --- /dev/null +++ b/.changeset/fair-berries-study.md @@ -0,0 +1,5 @@ +--- +'@astrojs/telemetry': patch +--- + +Wrap `JSON.parse` in `try`/`catch` diff --git a/packages/telemetry/src/config.ts b/packages/telemetry/src/config.ts index f0d855597..6efcb7fe5 100644 --- a/packages/telemetry/src/config.ts +++ b/packages/telemetry/src/config.ts @@ -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; |