From 184230b209551c102387524a19541f37a616deb4 Mon Sep 17 00:00:00 2001 From: animesh sahoo Date: Mon, 30 Mar 2026 06:16:11 +0530 Subject: [PATCH 1/2] added localization caching and removed redundant fromjs calls --- src/json-schema-errors.js | 11 ++++++----- src/localization.js | 26 +++++++++++++++++--------- 2 files changed, 23 insertions(+), 14 deletions(-) diff --git a/src/json-schema-errors.js b/src/json-schema-errors.js index ad8f3fb..785edfe 100644 --- a/src/json-schema-errors.js +++ b/src/json-schema-errors.js @@ -14,9 +14,11 @@ import { Localization } from "./localization.js"; /** @type API.jsonSchemaErrors */ export const jsonSchemaErrors = async (errorOutput, schemaUri, instance, options = {}) => { - const normalizedErrors = await normalizedOutput(instance, errorOutput, schemaUri); const rootInstance = Instance.fromJs(instance); - const localization = await Localization.forLocale(options.locale ?? "en-US"); + const [normalizedErrors, localization] = await Promise.all([ + normalizedOutput(rootInstance, errorOutput, schemaUri), + Localization.forLocale(options.locale ?? "en-US") + ]); return await getErrors(normalizedErrors, rootInstance, localization); }; @@ -28,12 +30,11 @@ export const setNormalizationHandler = (schemaUri, handler) => { normalizationHandlers[schemaUri] = handler; }; -/** @type (instance: API.Json, errorOutput: API.OutputUnit, subjectUri: string) => Promise */ -async function normalizedOutput(instance, errorOutput, subjectUri) { +/** @type (value: JsonNode, errorOutput: API.OutputUnit, subjectUri: string) => Promise */ +async function normalizedOutput(value, errorOutput, subjectUri) { const schema = await getSchema(subjectUri); const errorIndex = await constructErrorIndex(errorOutput, schema); const { schemaUri, ast } = await compile(schema); - const value = Instance.fromJs(instance); return evaluateSchema(schemaUri, value, { ast, errorIndex, plugins: [...ast.plugins] }); } diff --git a/src/localization.js b/src/localization.js index eb7fe16..7b7bead 100644 --- a/src/localization.js +++ b/src/localization.js @@ -6,6 +6,9 @@ import { FluentBundle, FluentResource } from "@fluent/bundle"; * @import { ContainsRange, Json } from "./index.d.ts" */ +/** @type Map> */ +const localizationCache = new Map(); + export class Localization { /** * @param {string} locale @@ -20,16 +23,21 @@ export class Localization { /** @type (locale: string) => Promise */ static async forLocale(locale) { - try { - const ftl = await readFile(`${import.meta.dirname}/translations/${locale}.ftl`, "utf-8"); - const resource = new FluentResource(ftl); - const bundle = new FluentBundle(locale); - bundle.addResource(resource); - - return new Localization(locale, bundle); - } catch (error) { - throw Error(`The ${locale} locale is not supported.`, { cause: error }); + if (!localizationCache.has(locale)) { + localizationCache.set(locale, (async () => { + try { + const ftl = await readFile(`${import.meta.dirname}/translations/${locale}.ftl`, "utf-8"); + const resource = new FluentResource(ftl); + const bundle = new FluentBundle(locale); + bundle.addResource(resource); + return new Localization(locale, bundle); + } catch (error) { + localizationCache.delete(locale); + throw Error(`The ${locale} locale is not supported.`, { cause: error }); + } + })()); } + return /** @type Promise */ (localizationCache.get(locale)); } /** @type (messageId: string, args: Record) => string */ From 2841d67c8a3abd5bfd0c3d0719e45f56fa8ae751 Mon Sep 17 00:00:00 2001 From: Jason Desrosiers Date: Wed, 1 Apr 2026 12:34:09 -0700 Subject: [PATCH 2/2] Cleanup localization cache implementation --- src/localization.js | 26 ++++++++++++-------------- 1 file changed, 12 insertions(+), 14 deletions(-) diff --git a/src/localization.js b/src/localization.js index 7b7bead..e810d76 100644 --- a/src/localization.js +++ b/src/localization.js @@ -6,7 +6,7 @@ import { FluentBundle, FluentResource } from "@fluent/bundle"; * @import { ContainsRange, Json } from "./index.d.ts" */ -/** @type Map> */ +/** @type Map */ const localizationCache = new Map(); export class Localization { @@ -24,20 +24,18 @@ export class Localization { /** @type (locale: string) => Promise */ static async forLocale(locale) { if (!localizationCache.has(locale)) { - localizationCache.set(locale, (async () => { - try { - const ftl = await readFile(`${import.meta.dirname}/translations/${locale}.ftl`, "utf-8"); - const resource = new FluentResource(ftl); - const bundle = new FluentBundle(locale); - bundle.addResource(resource); - return new Localization(locale, bundle); - } catch (error) { - localizationCache.delete(locale); - throw Error(`The ${locale} locale is not supported.`, { cause: error }); - } - })()); + try { + const ftl = await readFile(`${import.meta.dirname}/translations/${locale}.ftl`, "utf-8"); + const resource = new FluentResource(ftl); + const bundle = new FluentBundle(locale); + bundle.addResource(resource); + localizationCache.set(locale, new Localization(locale, bundle)); + } catch (error) { + throw Error(`The ${locale} locale is not supported.`, { cause: error }); + } } - return /** @type Promise */ (localizationCache.get(locale)); + + return /** @type Localization */ (localizationCache.get(locale)); } /** @type (messageId: string, args: Record) => string */