|
| 1 | +import axios from 'axios' |
| 2 | +import { REFINE_GLOSSARY_REMOTE, REFINE_GLOSSARY_TERMS } from '../../shared/constants' |
| 3 | + |
| 4 | +const COMMENT_PREFIX = '#' |
| 5 | +const FALLBACK_GLOSSARY_TERMS = [...REFINE_GLOSSARY_TERMS] |
| 6 | +const SUPPORTED_CONTENT_TYPES = ['text/plain', 'application/octet-stream'] as const |
| 7 | + |
| 8 | +function normalizeGlossaryTerms(terms: readonly string[]): string[] { |
| 9 | + return Array.from(new Set(terms.map((term) => term.trim()).filter((term) => term.length > 0))) |
| 10 | +} |
| 11 | + |
| 12 | +function getContentType(headers: unknown): string | null { |
| 13 | + if (!headers || typeof headers !== 'object') { |
| 14 | + return null |
| 15 | + } |
| 16 | + |
| 17 | + const contentType = (headers as Record<string, unknown>)['content-type'] |
| 18 | + if (typeof contentType === 'string') { |
| 19 | + return contentType |
| 20 | + } |
| 21 | + |
| 22 | + if (Array.isArray(contentType)) { |
| 23 | + const [firstContentType] = contentType |
| 24 | + return typeof firstContentType === 'string' ? firstContentType : null |
| 25 | + } |
| 26 | + |
| 27 | + return null |
| 28 | +} |
| 29 | + |
| 30 | +function isSupportedContentType(contentType: string | null): boolean { |
| 31 | + if (!contentType) { |
| 32 | + return true |
| 33 | + } |
| 34 | + |
| 35 | + const normalizedContentType = contentType.toLowerCase() |
| 36 | + return SUPPORTED_CONTENT_TYPES.some((supportedType) => |
| 37 | + normalizedContentType.includes(supportedType), |
| 38 | + ) |
| 39 | +} |
| 40 | + |
| 41 | +function parseRemoteGlossaryText(rawText: string): string[] { |
| 42 | + const normalizedText = rawText.replace(/^\uFEFF/, '') |
| 43 | + const glossaryTerms = normalizeGlossaryTerms( |
| 44 | + normalizedText |
| 45 | + .split(/\r?\n/u) |
| 46 | + .map((line) => line.trim()) |
| 47 | + .filter((line) => line.length > 0 && !line.startsWith(COMMENT_PREFIX)), |
| 48 | + ) |
| 49 | + |
| 50 | + if (glossaryTerms.length === 0) { |
| 51 | + throw new Error('Remote glossary is empty') |
| 52 | + } |
| 53 | + |
| 54 | + return glossaryTerms |
| 55 | +} |
| 56 | + |
| 57 | +export class RefineGlossaryCache { |
| 58 | + private glossaryTerms: string[] |
| 59 | + |
| 60 | + constructor() { |
| 61 | + this.glossaryTerms = normalizeGlossaryTerms(FALLBACK_GLOSSARY_TERMS) |
| 62 | + } |
| 63 | + |
| 64 | + getTerms(): readonly string[] { |
| 65 | + return this.glossaryTerms |
| 66 | + } |
| 67 | + |
| 68 | + resetToFallback(): void { |
| 69 | + this.glossaryTerms = normalizeGlossaryTerms(FALLBACK_GLOSSARY_TERMS) |
| 70 | + } |
| 71 | + |
| 72 | + async refreshFromRemote(): Promise<readonly string[]> { |
| 73 | + const response = await axios.get<string>(REFINE_GLOSSARY_REMOTE.URL, { |
| 74 | + headers: { |
| 75 | + Accept: 'text/plain', |
| 76 | + }, |
| 77 | + responseEncoding: 'utf8', |
| 78 | + responseType: 'text', |
| 79 | + timeout: REFINE_GLOSSARY_REMOTE.TIMEOUT_MS, |
| 80 | + }) |
| 81 | + |
| 82 | + const contentType = getContentType(response.headers) |
| 83 | + if (!isSupportedContentType(contentType)) { |
| 84 | + throw new Error(`Unexpected glossary content type: ${contentType}`) |
| 85 | + } |
| 86 | + |
| 87 | + if (typeof response.data !== 'string') { |
| 88 | + throw new Error('Remote glossary response is not plain text') |
| 89 | + } |
| 90 | + |
| 91 | + const glossaryTerms = parseRemoteGlossaryText(response.data) |
| 92 | + this.glossaryTerms = glossaryTerms |
| 93 | + |
| 94 | + return this.glossaryTerms |
| 95 | + } |
| 96 | +} |
0 commit comments