@visulima/fs
Version:
Human friendly file system utilities for Node.js
119 lines (114 loc) • 4.99 kB
JavaScript
;
Object.defineProperties(exports, { __esModule: { value: true }, [Symbol.toStringTag]: { value: 'Module' } });
const node_fs = require('node:fs');
const promises = require('node:fs/promises');
const node_path = require('node:path');
const node_stream = require('node:stream');
const node_url = require('node:url');
const node_zlib = require('node:zlib');
const utils = require('@visulima/path/utils');
var __defProp = Object.defineProperty;
var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
const fileExists = /* @__PURE__ */ __name((input) => {
if (node_path.isAbsolute(input)) {
return node_fs.existsSync(input);
}
try {
return node_fs.existsSync(node_path.resolve(input));
} catch {
return false;
}
}, "fileExists");
const getStreamSizeEfficient = /* @__PURE__ */ __name(async (stream) => {
let totalSize = 0;
for await (const chunk of stream) {
totalSize += Buffer.from(chunk).length;
}
return totalSize;
}, "getStreamSizeEfficient");
const getCompressedStreamSizeEfficient = /* @__PURE__ */ __name(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);
});
}, "getCompressedStreamSizeEfficient");
const processInputEfficiently = /* @__PURE__ */ __name(async (input, processor, streamProcessor) => {
if (input instanceof node_url.URL || typeof input === "string") {
const path = utils.toPath(input);
if (fileExists(path)) {
const fileStream = node_stream.Readable.from(await promises.readFile(path));
return await streamProcessor(fileStream);
}
if (typeof input === "string") {
return processor(Buffer.from(input));
}
}
if (input instanceof node_stream.Readable) {
return await streamProcessor(input);
}
return processor(input);
}, "processInputEfficiently");
const gzipSize = /* @__PURE__ */ __name(async (input, options) => {
const streamProcessor = /* @__PURE__ */ __name(async (stream) => await getCompressedStreamSizeEfficient(stream, () => node_zlib.createGzip(options)), "streamProcessor");
const bufferProcessor = /* @__PURE__ */ __name((data) => node_zlib.gzipSync(data, options).length, "bufferProcessor");
return await processInputEfficiently(input, bufferProcessor, streamProcessor);
}, "gzipSize");
const brotliSize = /* @__PURE__ */ __name(async (input, options) => {
const streamProcessor = /* @__PURE__ */ __name(async (stream) => await getCompressedStreamSizeEfficient(stream, () => node_zlib.createBrotliCompress(options)), "streamProcessor");
const bufferProcessor = /* @__PURE__ */ __name((data) => node_zlib.brotliCompressSync(data, options).length, "bufferProcessor");
return await processInputEfficiently(input, bufferProcessor, streamProcessor);
}, "brotliSize");
const rawSize = /* @__PURE__ */ __name(async (input) => {
const streamProcessor = /* @__PURE__ */ __name(async (stream) => await getStreamSizeEfficient(stream), "streamProcessor");
const bufferProcessor = /* @__PURE__ */ __name((data) => data.length, "bufferProcessor");
return await processInputEfficiently(input, bufferProcessor, streamProcessor);
}, "rawSize");
const gzipSizeSync = /* @__PURE__ */ __name((input, options) => {
if (input instanceof node_url.URL || typeof input === "string") {
const path = utils.toPath(input);
if (fileExists(path)) {
return node_zlib.gzipSync(node_fs.readFileSync(path), options).length;
}
if (typeof input === "string") {
return node_zlib.gzipSync(Buffer.from(input), options).length;
}
}
return node_zlib.gzipSync(input, options).length;
}, "gzipSizeSync");
const brotliSizeSync = /* @__PURE__ */ __name((input, options) => {
if (input instanceof node_url.URL || typeof input === "string") {
const path = utils.toPath(input);
if (fileExists(path)) {
return node_zlib.brotliCompressSync(node_fs.readFileSync(path), options).length;
}
if (typeof input === "string") {
return node_zlib.brotliCompressSync(Buffer.from(input), options).length;
}
}
return node_zlib.brotliCompressSync(input, options).length;
}, "brotliSizeSync");
const rawSizeSync = /* @__PURE__ */ __name((input) => {
if (input instanceof node_url.URL || typeof input === "string") {
const path = utils.toPath(input);
if (fileExists(path)) {
return node_fs.statSync(path).size;
}
if (typeof input === "string") {
return Buffer.from(input).length;
}
}
return input.length;
}, "rawSizeSync");
exports.brotliSize = brotliSize;
exports.brotliSizeSync = brotliSizeSync;
exports.gzipSize = gzipSize;
exports.gzipSizeSync = gzipSizeSync;
exports.rawSize = rawSize;
exports.rawSizeSync = rawSizeSync;