|
| 1 | +import { createHash } from 'node:crypto'; |
| 2 | +import fs from 'node:fs/promises'; |
| 3 | + |
| 4 | +import type { CSpellSettings, CSpellVFS } from '@cspell/cspell-types'; |
| 5 | +import { mergeConfig } from '@cspell/cspell-types'; |
| 6 | +import type { CSpellConfigFile, CSpellConfigFileReaderWriter, ICSpellConfigFile } from 'cspell-config-lib'; |
| 7 | + |
| 8 | +export class CSpellDictionaryBundler { |
| 9 | + #loadedConfigs = new Map<string, Promise<ICSpellConfigFile>>(); |
| 10 | + |
| 11 | + constructor(readonly reader: CSpellConfigFileReaderWriter) {} |
| 12 | + |
| 13 | + bundle(url: URL, content?: string): Promise<ICSpellConfigFile> { |
| 14 | + const found = this.#loadedConfigs.get(url.href); |
| 15 | + if (found) { |
| 16 | + return found; |
| 17 | + } |
| 18 | + const config = this.importConfig(url, content).then((config) => this.bundleConfig(config)); |
| 19 | + this.#loadedConfigs.set(url.href, config); |
| 20 | + return config; |
| 21 | + } |
| 22 | + |
| 23 | + async bundleConfig(config: CSpellConfigFile): Promise<ICSpellConfigFile> { |
| 24 | + const imports = await this.loadImports(config); |
| 25 | + const settings = mergeConfig( |
| 26 | + imports.map((f) => f.settings), |
| 27 | + await this.resolveDictionaries(config), |
| 28 | + ); |
| 29 | + delete settings.import; |
| 30 | + return { |
| 31 | + url: config.url, |
| 32 | + settings, |
| 33 | + }; |
| 34 | + } |
| 35 | + |
| 36 | + async resolveDictionaries(config: ICSpellConfigFile): Promise<CSpellSettings> { |
| 37 | + const settings = { ...config.settings }; |
| 38 | + if (!settings.dictionaryDefinitions) return settings; |
| 39 | + // Make a copy of the dictionary definitions and vfs to avoid mutating the original config file. |
| 40 | + const dictDefs = (settings.dictionaryDefinitions = [...settings.dictionaryDefinitions]); |
| 41 | + const vfs: CSpellVFS = (settings.vfs ??= Object.create(null)); |
| 42 | + |
| 43 | + for (let i = 0; i < dictDefs.length; ++i) { |
| 44 | + const def = dictDefs[i]; |
| 45 | + if (!def.path) continue; |
| 46 | + const d = { ...def }; |
| 47 | + dictDefs[i] = d; |
| 48 | + delete d.file; |
| 49 | + const url = new URL(def.btrie ?? def.path, config.url); |
| 50 | + if (url.protocol !== 'file:') continue; |
| 51 | + const vfsUrl = await populateVfs(vfs, url); |
| 52 | + d.path = vfsUrl.href; |
| 53 | + } |
| 54 | + |
| 55 | + return settings; |
| 56 | + } |
| 57 | + |
| 58 | + importConfig(url: URL, content?: string): Promise<CSpellConfigFile> { |
| 59 | + if (content) { |
| 60 | + return Promise.resolve(this.reader.parse({ url, content })); |
| 61 | + } |
| 62 | + return this.reader.readConfig(url); |
| 63 | + } |
| 64 | + |
| 65 | + loadImports(config: CSpellConfigFile): Promise<ICSpellConfigFile[]> { |
| 66 | + const imports = [config.settings.import || []].flat(); |
| 67 | + return Promise.all(imports.map((importPath) => this.bundle(new URL(importPath, config.url)))); |
| 68 | + } |
| 69 | +} |
| 70 | + |
| 71 | +/** |
| 72 | + * Load a file from the file system and populate the virtual file system with its content. |
| 73 | + * |
| 74 | + * @param vfs - The Virtual Files system data |
| 75 | + * @param url - The url to load and store. |
| 76 | + * @return The cspell-vfs url that was loaded. |
| 77 | + */ |
| 78 | +export async function populateVfs(vfs: CSpellVFS, url: URL): Promise<URL> { |
| 79 | + const content = await fs.readFile(url); |
| 80 | + |
| 81 | + const hash = createHash('sha256').update(content).digest('hex'); |
| 82 | + |
| 83 | + const data = content.toString('base64'); |
| 84 | + const vfsUrl = makeVfsUrl(url, hash.slice(0, 16)); |
| 85 | + vfs[vfsUrl.href] = { |
| 86 | + data, |
| 87 | + encoding: 'base64', |
| 88 | + }; |
| 89 | + return vfsUrl; |
| 90 | +} |
| 91 | + |
| 92 | +/** |
| 93 | + * We want to make a url that is unique to the content of the file and indications where it came from. |
| 94 | + * @param url - the url to the source file. |
| 95 | + * @param hash - the hash of the file content |
| 96 | + * @returns A `cspell-vfs:///` url that can be used to reference the file content in the virtual file system. |
| 97 | + */ |
| 98 | +export function makeVfsUrl(url: URL, hash: string): URL { |
| 99 | + const parts = url.pathname.split('/').slice(1); |
| 100 | + let pos = -3; |
| 101 | + const i = parts.lastIndexOf('node_modules'); |
| 102 | + if (i > 0) { |
| 103 | + pos = i + 1; |
| 104 | + } |
| 105 | + const path = parts.slice(pos).join('/'); |
| 106 | + return new URL(`cspell-vfs:///${hash}/${path}`); |
| 107 | +} |
0 commit comments