|
| 1 | +import { urlOrReferenceToUrl } from '../common/index.js'; |
| 2 | +import type { Disposable } from '../models/index.js'; |
| 3 | +import { type FileReference, type FileResource, FileType, type Stats } from '../models/index.js'; |
| 4 | +import { VFSNotFoundError, VFSNotSupported } from './errors.js'; |
| 5 | +import type { FileSystemProviderInfo, UrlOrReference, VfsDirEntry } from './VFileSystem.js'; |
| 6 | +import { FSCapabilityFlags } from './VFileSystem.js'; |
| 7 | +import type { VFileSystemProvider, VProviderFileSystem, VProviderFileSystemReadFileOptions } from './VirtualFS.js'; |
| 8 | + |
| 9 | +export class MemFileSystemProvider implements VFileSystemProvider, Disposable { |
| 10 | + readonly name: string; |
| 11 | + readonly protocol: string; |
| 12 | + #vfs: MemVFileSystem; |
| 13 | + |
| 14 | + /** |
| 15 | + * @param name - the name of the provider, used for debugging and logging. |
| 16 | + * @param protocol - the protocol (end with a :), examples: `vfs:`, `cspell-vfs:` |
| 17 | + */ |
| 18 | + constructor(name: string, protocol: string) { |
| 19 | + this.name = name; |
| 20 | + this.protocol = protocol; |
| 21 | + this.#vfs = new MemVFileSystem(name, protocol); |
| 22 | + } |
| 23 | + |
| 24 | + getFileSystem(url: URL): VProviderFileSystem | undefined { |
| 25 | + if (url.protocol !== this.protocol) { |
| 26 | + return undefined; |
| 27 | + } |
| 28 | + return this.#vfs; |
| 29 | + } |
| 30 | + |
| 31 | + get memFS(): MemVFileSystem { |
| 32 | + return this.#vfs; |
| 33 | + } |
| 34 | + |
| 35 | + dispose(): void { |
| 36 | + this.#vfs.dispose(); |
| 37 | + } |
| 38 | + |
| 39 | + [Symbol.dispose](): void { |
| 40 | + this.dispose(); |
| 41 | + } |
| 42 | +} |
| 43 | + |
| 44 | +interface MemVFileSystemEntry { |
| 45 | + file: FileResource; |
| 46 | + stats: Stats; |
| 47 | +} |
| 48 | + |
| 49 | +export class MemVFileSystem implements VProviderFileSystem { |
| 50 | + readonly name: string; |
| 51 | + readonly protocol: string; |
| 52 | + readonly capabilities: FSCapabilityFlags = FSCapabilityFlags.ReadWrite | FSCapabilityFlags.Stat; |
| 53 | + #files: Map<string, MemVFileSystemEntry> = new Map(); |
| 54 | + |
| 55 | + constructor(name: string, protocol: string) { |
| 56 | + this.name = name; |
| 57 | + this.protocol = protocol; |
| 58 | + this.providerInfo = { name }; |
| 59 | + this.#files = new Map(); |
| 60 | + } |
| 61 | + |
| 62 | + /** |
| 63 | + * Read a file. |
| 64 | + * @param url - URL to read |
| 65 | + * @param options - options for reading the file. |
| 66 | + * @returns A FileResource, the content will not be decoded. Use `.getText()` to get the decoded text. |
| 67 | + */ |
| 68 | + async readFile(url: UrlOrReference, _options?: VProviderFileSystemReadFileOptions): Promise<FileResource> { |
| 69 | + return this.#getEntryOrThrow(url).file; |
| 70 | + } |
| 71 | + |
| 72 | + /** |
| 73 | + * Write a file |
| 74 | + * @param file - the file to write |
| 75 | + */ |
| 76 | + async writeFile(file: FileResource): Promise<FileReference> { |
| 77 | + const stats: Stats = { |
| 78 | + size: file.content.length, |
| 79 | + mtimeMs: performance.now(), |
| 80 | + fileType: FileType.File, |
| 81 | + }; |
| 82 | + const u = urlOrReferenceToUrl(file); |
| 83 | + this.#files.set(u.href, { file, stats }); |
| 84 | + return { url: file.url }; |
| 85 | + } |
| 86 | + |
| 87 | + /** |
| 88 | + * Get the stats for a url. |
| 89 | + * @param url - Url to fetch stats for. |
| 90 | + */ |
| 91 | + stat(url: UrlOrReference): Stats { |
| 92 | + return this.#getEntryOrThrow(url).stats; |
| 93 | + } |
| 94 | + |
| 95 | + #getEntryOrThrow(url: UrlOrReference): MemVFileSystemEntry { |
| 96 | + const u = urlOrReferenceToUrl(url); |
| 97 | + const found = this.#files.get(u.href); |
| 98 | + if (!found) { |
| 99 | + throw new VFSNotFoundError(u); |
| 100 | + } |
| 101 | + return found; |
| 102 | + } |
| 103 | + |
| 104 | + /** |
| 105 | + * Read the directory entries for a url. |
| 106 | + * The url should end with `/` to indicate a directory. |
| 107 | + * @param url - the url to read the directory entries for. |
| 108 | + */ |
| 109 | + async readDirectory(url: URL): Promise<VfsDirEntry[]> { |
| 110 | + throw new VFSNotSupported('readDirectory', url); |
| 111 | + } |
| 112 | + |
| 113 | + /** |
| 114 | + * Information about the provider. |
| 115 | + * It is up to the provider to define what information is available. |
| 116 | + */ |
| 117 | + readonly providerInfo: FileSystemProviderInfo; |
| 118 | + readonly hasProvider: boolean = true; |
| 119 | + |
| 120 | + dispose(): void { |
| 121 | + this.#files.clear(); |
| 122 | + } |
| 123 | + |
| 124 | + [Symbol.dispose](): void { |
| 125 | + this.dispose(); |
| 126 | + } |
| 127 | +} |
0 commit comments