@visulima/fs
Version:
Human friendly file system utilities for Node.js
145 lines (138 loc) • 4.76 kB
JavaScript
import { createRequire as __cjs_createRequire } from "node:module";
const __cjs_require = __cjs_createRequire(import.meta.url);
const __cjs_getProcess = typeof globalThis !== "undefined" && typeof globalThis.process !== "undefined" ? globalThis.process : process;
const __cjs_getBuiltinModule = (module) => {
// Check if we're in Node.js and version supports getBuiltinModule
if (typeof __cjs_getProcess !== "undefined" && __cjs_getProcess.versions && __cjs_getProcess.versions.node) {
const [major, minor] = __cjs_getProcess.versions.node.split(".").map(Number);
// Node.js 20.16.0+ and 22.3.0+
if (major > 22 || (major === 22 && minor >= 3) || (major === 20 && minor >= 16)) {
return __cjs_getProcess.getBuiltinModule(module);
}
}
// Fallback to createRequire
return __cjs_require(module);
};
const {
readFileSync,
statSync,
existsSync
} = __cjs_getBuiltinModule("node:fs");
const {
readFile
} = __cjs_getBuiltinModule("node:fs/promises");
const {
isAbsolute,
resolve
} = __cjs_getBuiltinModule("node:path");
const {
Readable
} = __cjs_getBuiltinModule("node:stream");
const {
URL
} = __cjs_getBuiltinModule("node:url");
const {
gzipSync,
brotliCompressSync,
createBrotliCompress,
createGzip
} = __cjs_getBuiltinModule("node:zlib");
import { toPath } from '@visulima/path/utils';
const fileExists = (input) => {
if (isAbsolute(input)) {
return existsSync(input);
}
try {
return existsSync(resolve(input));
} catch {
return false;
}
};
const getStreamSizeEfficient = async (stream) => {
let totalSize = 0;
for await (const chunk of stream) {
totalSize += Buffer.from(chunk).length;
}
return totalSize;
};
const getCompressedStreamSizeEfficient = async (stream, createCompressor) => {
let totalSize = 0;
const compressor = createCompressor();
return await new Promise((resolve2, reject) => {
compressor.on("data", (chunk) => {
totalSize += chunk.length;
});
compressor.on("end", () => resolve2(totalSize));
compressor.on("error", reject);
stream.on("error", reject);
stream.pipe(compressor);
});
};
const processInputEfficiently = async (input, processor, streamProcessor) => {
if (input instanceof URL || typeof input === "string") {
const path = toPath(input);
if (fileExists(path)) {
const fileStream = Readable.from(await readFile(path));
return await streamProcessor(fileStream);
}
if (typeof input === "string") {
return processor(Buffer.from(input));
}
}
if (input instanceof Readable) {
return await streamProcessor(input);
}
return processor(input);
};
const gzipSize = async (input, options) => {
const streamProcessor = async (stream) => await getCompressedStreamSizeEfficient(stream, () => createGzip(options));
const bufferProcessor = (data) => gzipSync(data, options).length;
return await processInputEfficiently(input, bufferProcessor, streamProcessor);
};
const brotliSize = async (input, options) => {
const streamProcessor = async (stream) => await getCompressedStreamSizeEfficient(stream, () => createBrotliCompress(options));
const bufferProcessor = (data) => brotliCompressSync(data, options).length;
return await processInputEfficiently(input, bufferProcessor, streamProcessor);
};
const rawSize = async (input) => {
const streamProcessor = async (stream) => await getStreamSizeEfficient(stream);
const bufferProcessor = (data) => data.length;
return await processInputEfficiently(input, bufferProcessor, streamProcessor);
};
const gzipSizeSync = (input, options) => {
if (input instanceof URL || typeof input === "string") {
const path = toPath(input);
if (fileExists(path)) {
return gzipSync(readFileSync(path), options).length;
}
if (typeof input === "string") {
return gzipSync(Buffer.from(input), options).length;
}
}
return gzipSync(input, options).length;
};
const brotliSizeSync = (input, options) => {
if (input instanceof URL || typeof input === "string") {
const path = toPath(input);
if (fileExists(path)) {
return brotliCompressSync(readFileSync(path), options).length;
}
if (typeof input === "string") {
return brotliCompressSync(Buffer.from(input), options).length;
}
}
return brotliCompressSync(input, options).length;
};
const rawSizeSync = (input) => {
if (input instanceof URL || typeof input === "string") {
const path = toPath(input);
if (fileExists(path)) {
return statSync(path).size;
}
if (typeof input === "string") {
return Buffer.from(input).length;
}
}
return input.length;
};
export { brotliSize, brotliSizeSync, gzipSize, gzipSizeSync, rawSize, rawSizeSync };