refactor: change used key for pronunciation cache to be independent of requesting locale, only on used voice

This commit is contained in:
Valentyne Stigloher 2025-09-14 19:56:46 +02:00
parent 228798d103
commit 85c6e38d1b

View File

@ -3,16 +3,15 @@ import { NoSuchKey } from '@aws-sdk/client-s3';
import type { S3 } from '@aws-sdk/client-s3';
import type { NodeJsClient } from '@smithy/types';
import { Base64 } from 'js-base64';
import sha1 from 'sha1';
import {
convertPronunciationStringToSsml,
convertPronunciationStringToNarakeetFormat,
sha256,
} from '#shared/helpers.ts';
import { voices } from '#shared/pronunciation/voices.ts';
import type { VoiceKey, AwsPollyVoice, NarakeetVoice, Voice } from '#shared/pronunciation/voices.ts';
import { s3, awsConfig, s3BucketParams } from '~~/server/cloudServices.ts';
import { getLocale, loadConfig, localesDict } from '~~/server/data.ts';
type ProviderKey = 'aws_polly' | 'narakeet';
interface Provider {
@ -82,9 +81,6 @@ export default defineEventHandler(async (event) => {
});
}
const locale = getLocale(event);
const config = await loadConfig(locale);
const voiceKey = getRouterParam(event, 'voice');
if (voiceKey === undefined || !(voiceKey in voices)) {
throw createError({
@ -96,7 +92,14 @@ export default defineEventHandler(async (event) => {
const voice = voices[voiceKey as VoiceKey];
const provider = providers[(voice.provider || 'aws_polly') as ProviderKey];
const tokenised = provider.tokenised(text);
const key = `pronunciation/${config.locale}-${voiceKey}/${sha1(tokenised)}.mp3`;
const key = `pronunciation/${voiceKey}/${await sha256(tokenised)}.mp3`;
// bypass cache in development
if (import.meta.dev) {
const [buffer, contentType] = await provider.generate(tokenised, voice);
setResponseHeader(event, 'content-type', contentType);
return buffer;
}
try {
const s3Response = await (s3 as NodeJsClient<S3>).getObject({ Key: key, ...s3BucketParams });
@ -109,15 +112,13 @@ export default defineEventHandler(async (event) => {
const [buffer, contentType] = await provider.generate(tokenised, voice);
if (localesDict[locale]?.published) {
// don't populate the cache for locales still under construction
await s3.putObject({
Key: key,
Body: buffer,
ContentType: contentType,
...s3BucketParams,
});
}
// don't populate the cache for locales still under construction
await s3.putObject({
Key: key,
Body: buffer,
ContentType: contentType,
...s3BucketParams,
});
setResponseHeader(event, 'content-type', contentType);
return buffer;