diff options
author | toasted-nutbread <toasted-nutbread@users.noreply.github.com> | 2023-12-19 00:33:38 -0500 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-12-19 05:33:38 +0000 |
commit | 1ced9aafc00c10992bab8bd3f1b6b1397f05b7b9 (patch) | |
tree | 305bb2b3bfc7fc3b051ee1cd3d1c35f442af0de4 /ext/js/language | |
parent | 5f96276fda93dcad39f2165fd3c8d890aa5f9be5 (diff) |
Make JSON.parse usage safer (#373)
* Make JSON.parse usage safer
* Fix any type
* Add readResponseJson
* Use readResponseJson
* Additional updates
* Rename files
* Add types
Diffstat (limited to 'ext/js/language')
-rw-r--r-- | ext/js/language/deinflector.js | 2 | ||||
-rw-r--r-- | ext/js/language/dictionary-importer.js | 24 |
2 files changed, 4 insertions, 22 deletions
diff --git a/ext/js/language/deinflector.js b/ext/js/language/deinflector.js index 676f45a1..26cc6b18 100644 --- a/ext/js/language/deinflector.js +++ b/ext/js/language/deinflector.js @@ -20,7 +20,7 @@ export class Deinflector { /** * @param {import('deinflector').ReasonsRaw} reasons * @example - * const deinflectionReasons = JSON.parse( + * const deinflectionReasons = parseJson( * readFileSync(path.join('ext/data/deinflect.json')).toString(), * ); * const deinflector = new Deinflector(deinflectionReasons); 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); @@ -589,25 +590,6 @@ export class DictionaryImporter { } /** - * @param {string} url - * @returns {Promise<unknown>} - */ - 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 * @returns {import('dictionary-database').DatabaseTermEntry} @@ -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(); |