@visulima/fs
Version:
Human friendly file system utilities for Node.js
110 lines (107 loc) • 4.57 kB
JavaScript
import { readFileSync, statSync, existsSync } from 'node:fs';
import { readFile } from 'node:fs/promises';
import { isAbsolute, resolve } from 'node:path';
import { Readable } from 'node:stream';
import { URL } from 'node:url';
import { createBrotliCompress, brotliCompressSync, gzipSync, createGzip } from 'node:zlib';
import { toPath } from '@visulima/path/utils';
var __defProp = Object.defineProperty;
var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
const fileExists = /* @__PURE__ */ __name((input) => {
if (isAbsolute(input)) {
return existsSync(input);
}
try {
return existsSync(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 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);
}, "processInputEfficiently");
const gzipSize = /* @__PURE__ */ __name(async (input, options) => {
const streamProcessor = /* @__PURE__ */ __name(async (stream) => await getCompressedStreamSizeEfficient(stream, () => createGzip(options)), "streamProcessor");
const bufferProcessor = /* @__PURE__ */ __name((data) => 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, () => createBrotliCompress(options)), "streamProcessor");
const bufferProcessor = /* @__PURE__ */ __name((data) => 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 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;
}, "gzipSizeSync");
const brotliSizeSync = /* @__PURE__ */ __name((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;
}, "brotliSizeSync");
const rawSizeSync = /* @__PURE__ */ __name((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;
}, "rawSizeSync");
export { brotliSize, brotliSizeSync, gzipSize, gzipSizeSync, rawSize, rawSizeSync };