@dbs-portal/core-module-registry
Version:
Core module registry system for automatic module discovery and registration
186 lines • 5.93 kB
JavaScript
/**
* Browser-compatible file system utilities
*
* Provides file system functions that work in browser environments
* while maintaining compatibility with Node.js fs/promises API.
*/
class BrowserFileSystem {
cache = new Map();
mockFiles = new Map();
/**
* Read file content
* In browser environment, this will work with pre-loaded or cached content
*/
async readFile(path, _encoding = 'utf8') {
// Check cache first
if (this.cache.has(path)) {
return this.cache.get(path);
}
// Check mock files
if (this.mockFiles.has(path)) {
return this.mockFiles.get(path);
}
// In browser, try to fetch if it's a URL
if (this.isUrl(path)) {
try {
const response = await fetch(path);
if (!response.ok) {
throw new Error(`Failed to fetch ${path}: ${response.statusText}`);
}
const content = await response.text();
this.cache.set(path, content);
return content;
}
catch (error) {
throw new Error(`ENOENT: no such file or directory, open '${path}'`);
}
}
// For browser environment, we can't actually read arbitrary files
// This would typically be handled by the build system or pre-loading
throw new Error(`ENOENT: no such file or directory, open '${path}' (browser environment)`);
}
/**
* Write file content
* In browser environment, this stores in memory cache
*/
async writeFile(path, data, _encoding = 'utf8') {
this.mockFiles.set(path, data);
this.cache.set(path, data);
}
/**
* Check file access
*/
async access(path) {
if (!await this.exists(path)) {
throw new Error(`ENOENT: no such file or directory, access '${path}'`);
}
}
/**
* Get file stats
*/
async stat(path) {
if (!await this.exists(path)) {
throw new Error(`ENOENT: no such file or directory, stat '${path}'`);
}
const content = this.mockFiles.get(path) || this.cache.get(path) || '';
const now = new Date();
return {
isFile: () => true,
isDirectory: () => false,
size: content.length,
mtime: now,
ctime: now
};
}
/**
* Read directory contents
*/
async readdir(path) {
// In browser environment, we can't actually read directories
// This would typically be handled by the build system or pre-loading
const files = [];
// Check for cached directory listings
const dirKey = `__dir__${path}`;
if (this.cache.has(dirKey)) {
return this.cache.get(dirKey);
}
// Return mock directory contents based on known files
for (const [filePath] of this.mockFiles) {
if (filePath.startsWith(path) && filePath !== path) {
const relativePath = filePath.slice(path.length + 1);
const firstSegment = relativePath.split('/')[0];
if (firstSegment && !files.includes(firstSegment)) {
files.push(firstSegment);
}
}
}
return files;
}
/**
* Create directory
*/
async mkdir(path, _options = {}) {
// In browser environment, just mark as created
const dirKey = `__dir__${path}`;
this.cache.set(dirKey, []);
}
/**
* Check if file exists
*/
async exists(path) {
return this.cache.has(path) ||
this.mockFiles.has(path) ||
this.cache.has(`__dir__${path}`) ||
(this.isUrl(path) && await this.checkUrlExists(path));
}
/**
* Pre-load file content (for browser usage)
*/
preloadFile(path, content) {
this.cache.set(path, content);
}
/**
* Pre-load directory listing (for browser usage)
*/
preloadDirectory(path, files) {
const dirKey = `__dir__${path}`;
this.cache.set(dirKey, files);
}
/**
* Set mock file content (for testing)
*/
setMockFile(path, content) {
this.mockFiles.set(path, content);
}
/**
* Clear all cached content
*/
clearCache() {
this.cache.clear();
this.mockFiles.clear();
}
/**
* Check if path is a URL
*/
isUrl(path) {
try {
new URL(path);
return true;
}
catch {
return false;
}
}
/**
* Check if URL exists
*/
async checkUrlExists(url) {
try {
const response = await fetch(url, { method: 'HEAD' });
return response.ok;
}
catch {
return false;
}
}
}
// Create browser file system instance
const browserFs = new BrowserFileSystem();
// Export individual functions for compatibility
export const readFile = browserFs.readFile.bind(browserFs);
export const writeFile = browserFs.writeFile.bind(browserFs);
export const access = browserFs.access.bind(browserFs);
export const stat = browserFs.stat.bind(browserFs);
export const readdir = browserFs.readdir.bind(browserFs);
export const mkdir = browserFs.mkdir.bind(browserFs);
// Additional utility functions
export const exists = browserFs.exists.bind(browserFs);
export const preloadFile = browserFs.preloadFile.bind(browserFs);
export const preloadDirectory = browserFs.preloadDirectory.bind(browserFs);
export const setMockFile = browserFs.setMockFile.bind(browserFs);
export const clearCache = browserFs.clearCache.bind(browserFs);
// Export default object for compatibility
export default browserFs;
// Export the class for advanced usage
export { BrowserFileSystem };
//# sourceMappingURL=browser-fs.js.map