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/language/dictionary-importer.js | 24 +++--------------------- 1 file changed, 3 insertions(+), 21 deletions(-) (limited to 'ext/js/language/dictionary-importer.js') diff --git a/ext/js/language/dictionary-importer.js b/ext/js/language/dictionary-importer.js index dfbd9590..df9c48f1 100644 --- a/ext/js/language/dictionary-importer.js +++ b/ext/js/language/dictionary-importer.js @@ -26,6 +26,7 @@ import { } from '../../lib/zip.js'; import {stringReverse} from '../core.js'; import {ExtensionError} from '../core/extension-error.js'; +import {parseJson} from '../core/json.js'; import {MediaUtil} from '../media/media-util.js'; const ajvSchemas = /** @type {import('dictionary-importer').CompiledSchemaValidators} */ (/** @type {unknown} */ (ajvSchemas0)); @@ -89,7 +90,7 @@ export class DictionaryImporter { const indexFile2 = /** @type {import('@zip.js/zip.js').Entry} */ (indexFile); const indexContent = await this._getData(indexFile2, new TextWriter()); - const index = /** @type {import('dictionary-data').Index} */ (JSON.parse(indexContent)); + const index = /** @type {import('dictionary-data').Index} */ (parseJson(indexContent)); if (!ajvSchemas.dictionaryIndex(index)) { throw this._formatAjvSchemaError(ajvSchemas.dictionaryIndex, indexFileName); @@ -588,25 +589,6 @@ export class DictionaryImporter { return mediaData; } - /** - * @param {string} url - * @returns {Promise} - */ - async _fetchJsonAsset(url) { - const response = await fetch(url, { - method: 'GET', - mode: 'no-cors', - cache: 'default', - credentials: 'omit', - redirect: 'follow', - referrerPolicy: 'no-referrer' - }); - if (!response.ok) { - throw new Error(`Failed to fetch ${url}: ${response.status}`); - } - return await response.json(); - } - /** * @param {import('dictionary-data').TermV1} entry * @param {string} dictionary @@ -730,7 +712,7 @@ export class DictionaryImporter { const results = []; for (const file of files) { const content = await this._getData(file, new TextWriter()); - const entries = /** @type {unknown} */ (JSON.parse(content)); + const entries = /** @type {unknown} */ (parseJson(content)); startIndex = progressData.index; this._progress(); -- cgit v1.2.3