From 0ce0720c7f2c7ba21dddfea0b75d1e9b39c6a274 Mon Sep 17 00:00:00 2001 From: Nate Moore Date: Fri, 1 Sep 2023 14:19:12 -0500 Subject: [PATCH] Wrap `JSON.parse` in `try`/`catch` (#8363) * fix(telemetry): wrap JSON.parse in try/catch * fix: always write the store * chore(lint): fix --- .changeset/fair-berries-study.md | 5 +++++ packages/telemetry/src/config.ts | 12 +++++++----- 2 files changed, 12 insertions(+), 5 deletions(-) create mode 100644 .changeset/fair-berries-study.md 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) { this._store = value;