Constant NODE_FS_STUB
Source pub const NODE_FS_STUB: &str = r#"
// ESM-compatible bridge for node:fs to Deno APIs
const readFileSync = (path, encoding) => {
try {
if (globalThis.Deno?.readTextFileSync) {
const content = globalThis.Deno.readTextFileSync(path);
if (encoding === 'utf-8' || encoding === 'utf8') {
return content;
}
return new TextEncoder().encode(content);
}
return new Uint8Array(0);
} catch (error) {
throw new Error(`ENOENT: no such file or directory, open '${path}'`);
}
};
const readFile = async (path, encoding) => {
try {
if (globalThis.Deno?.readTextFile) {
const content = await globalThis.Deno.readTextFile(path);
if (encoding === 'utf-8' || encoding === 'utf8') {
return content;
}
return new TextEncoder().encode(content);
}
return new Uint8Array(0);
} catch (error) {
throw new Error(`ENOENT: no such file or directory, open '${path}'`);
}
};
const existsSync = (path) => {
try {
if (globalThis.Deno?.statSync) {
globalThis.Deno.statSync(path);
return true;
}
return false;
} catch (error) {
return false;
}
};
const statSync = (path) => {
try {
if (globalThis.Deno?.statSync) {
const stat = globalThis.Deno.statSync(path);
return {
isFile: () => stat.isFile,
isDirectory: () => stat.isDirectory,
isSymbolicLink: () => stat.isSymlink,
size: stat.size,
mtime: stat.mtime,
atime: stat.atime,
birthtime: stat.birthtime,
mode: stat.mode || 0,
uid: stat.uid || 0,
gid: stat.gid || 0,
};
}
throw new Error(`ENOENT: no such file or directory, stat '${path}'`);
} catch (error) {
throw new Error(`ENOENT: no such file or directory, stat '${path}'`);
}
};
const lstatSync = statSync;
const realpathSync = (path) => {
try {
if (globalThis.Deno?.realPathSync) {
return globalThis.Deno.realPathSync(path);
}
return path;
} catch (error) {
return path;
}
};
const constants = {
F_OK: 0,
R_OK: 4,
W_OK: 2,
X_OK: 1,
};
const readdirSync = (path) => {
try {
if (globalThis.Deno?.readDirSync) {
const entries = [];
for (const entry of globalThis.Deno.readDirSync(path)) {
entries.push(entry.name);
}
return entries;
}
return [];
} catch (error) {
throw new Error(`ENOENT: no such file or directory, scandir '${path}'`);
}
};
export default {
readFileSync,
readFile,
existsSync,
readdirSync,
statSync,
lstatSync,
realpathSync,
constants,
writeFileSync: () => {},
writeFile: () => Promise.resolve(),
exists: () => Promise.resolve(false),
mkdirSync: () => {},
mkdir: () => Promise.resolve(),
readdir: () => Promise.resolve([]),
};
const promises = {
readFile,
writeFile: () => Promise.resolve(),
readdir: () => Promise.resolve([]),
stat: async (path) => {
try {
if (globalThis.Deno?.stat) {
const stat = await globalThis.Deno.stat(path);
return {
isFile: () => stat.isFile,
isDirectory: () => stat.isDirectory,
isSymbolicLink: () => stat.isSymlink,
size: stat.size,
mtime: stat.mtime,
atime: stat.atime,
birthtime: stat.birthtime,
};
}
throw new Error(`ENOENT: no such file or directory, stat '${path}'`);
} catch (error) {
throw new Error(`ENOENT: no such file or directory, stat '${path}'`);
}
},
mkdir: () => Promise.resolve(),
rm: () => Promise.resolve(),
rmdir: () => Promise.resolve(),
unlink: () => Promise.resolve(),
access: () => Promise.resolve(),
};
const stat = async (path, callback) => {
try {
if (globalThis.Deno?.stat) {
const statResult = await globalThis.Deno.stat(path);
const result = {
isFile: () => statResult.isFile,
isDirectory: () => statResult.isDirectory,
isSymbolicLink: () => statResult.isSymlink,
size: statResult.size,
mtime: statResult.mtime,
atime: statResult.atime,
birthtime: statResult.birthtime,
};
if (callback) callback(null, result);
return result;
}
const error = new Error(`ENOENT: no such file or directory, stat '${path}'`);
if (callback) callback(error);
throw error;
} catch (error) {
if (callback) callback(error);
throw error;
}
};
const watchFile = (path, options, listener) => {
if (typeof options === 'function') {
listener = options;
}
console.warn('fs.watchFile is not supported in this environment');
return { close: () => {} };
};
const unwatchFile = (path, listener) => {
console.warn('fs.unwatchFile is not supported in this environment');
};
const watch = (path, options, listener) => {
if (typeof options === 'function') {
listener = options;
}
console.warn('fs.watch is not supported in this environment');
return { close: () => {} };
};
export { readFileSync, readFile, existsSync, readdirSync, statSync, lstatSync, realpathSync, constants, promises, stat, watchFile, unwatchFile, watch };
export const writeFileSync = () => {};
export const writeFile = () => Promise.resolve();
export const exists = () => Promise.resolve(false);
export const mkdirSync = () => {};
export const mkdir = () => Promise.resolve();
export const readdir = () => Promise.resolve([]);
export const __esModule = true;
"#;