From 1ced9aafc00c10992bab8bd3f1b6b1397f05b7b9 Mon Sep 17 00:00:00 2001 From: toasted-nutbread Date: Tue, 19 Dec 2023 00:33:38 -0500 Subject: Make JSON.parse usage safer (#373) * Make JSON.parse usage safer * Fix any type * Add readResponseJson * Use readResponseJson * Additional updates * Rename files * Add types --- ext/js/data/options-util.js | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) (limited to 'ext/js/data/options-util.js') diff --git a/ext/js/data/options-util.js b/ext/js/data/options-util.js index c3c0e685..a17763e9 100644 --- a/ext/js/data/options-util.js +++ b/ext/js/data/options-util.js @@ -17,6 +17,7 @@ */ import {escapeRegExp, isObject} from '../core.js'; +import {parseJson, readResponseJson} from '../core/json.js'; import {TemplatePatcher} from '../templates/template-patcher.js'; import {JsonSchema} from './json-schema.js'; @@ -30,7 +31,8 @@ export class OptionsUtil { /** */ async prepare() { - const schema = /** @type {import('json-schema').Schema} */ (await this._fetchJson('/data/schemas/options-schema.json')); + /** @type {import('json-schema').Schema} */ + const schema = await this._fetchJson('/data/schemas/options-schema.json'); this._optionsSchema = new JsonSchema(schema); } @@ -115,7 +117,7 @@ export class OptionsUtil { } }); }); - options = JSON.parse(optionsStr); + options = parseJson(optionsStr); } catch (e) { // NOP } @@ -477,12 +479,13 @@ export class OptionsUtil { } /** + * @template [T=unknown] * @param {string} url - * @returns {Promise} + * @returns {Promise} */ async _fetchJson(url) { const response = await this._fetchGeneric(url); - return await response.json(); + return await readResponseJson(response); } /** -- cgit v1.2.3