@poppinss/file-generator
Version:
Generate in-memory fake files for custom size
105 lines (104 loc) • 3.14 kB
JavaScript
import { dirname, join } from "node:path";
import { randomUUID } from "node:crypto";
import { readFile } from "node:fs/promises";
import bytes from "bytes";
import { fileURLToPath } from "node:url";
const E_INVALID_FILE_SIZE_EXPRESSION = (fileSize) => {
return /* @__PURE__ */ new Error(`Invalid fileSize value "${fileSize}". Expected value to be a number representing bytes or a string expression`);
};
function toBuffer(fileSize, contents) {
const size = typeof fileSize === "string" ? bytes(fileSize) : fileSize;
if (size === null) throw E_INVALID_FILE_SIZE_EXPRESSION(fileSize);
const contentsSize = contents.length;
return Buffer.alloc(size < contentsSize ? contentsSize : size, contents, "binary");
}
function dirname$1(url) {
return dirname(fileURLToPath(url));
}
async function generateDocx(fileSize, fileName) {
const mime = "application/vnd.openxmlformats-officedocument.wordprocessingml.document";
const name = fileName || `${randomUUID()}.docx`;
const contents = toBuffer(fileSize, await readFile(join(dirname$1(import.meta.url), "./fake.docx")));
return {
contents,
mime,
size: contents.length,
name
};
}
async function generateGif(fileSize, fileName) {
const mime = "image/gif";
const name = fileName || `${randomUUID()}.gif`;
const contents = toBuffer(fileSize, await readFile(join(dirname$1(import.meta.url), "./fake.gif")));
return {
contents,
mime,
size: contents.length,
name
};
}
async function generateJpg(fileSize, fileName) {
const mime = "image/jpeg";
const name = fileName || `${randomUUID()}.jpg`;
const contents = toBuffer(fileSize, await readFile(join(dirname$1(import.meta.url), "./fake.jpg")));
return {
contents,
mime,
size: contents.length,
name
};
}
async function generatePdf(fileSize, fileName) {
const mime = "application/pdf";
const name = fileName || `${randomUUID()}.pdf`;
const contents = toBuffer(fileSize, await readFile(join(dirname$1(import.meta.url), "./fake.pdf")));
return {
contents,
mime,
size: contents.length,
name
};
}
async function generatePng(fileSize, fileName) {
const mime = "image/png";
const name = fileName || `${randomUUID()}.png`;
const contents = toBuffer(fileSize, await readFile(join(dirname$1(import.meta.url), "./fake.png")));
return {
contents,
mime,
size: contents.length,
name
};
}
async function generateXlsx(fileSize, fileName) {
const mime = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
const name = fileName || `${randomUUID()}.xlsx`;
const contents = toBuffer(fileSize, await readFile(join(dirname$1(import.meta.url), "./fake.xlsx")));
return {
contents,
mime,
size: contents.length,
name
};
}
async function generateCsv(fileSize, fileName) {
const mime = "text/csv";
const name = fileName ?? `${randomUUID()}.csv`;
const contents = toBuffer(fileSize, Buffer.from("h1,h2,h3\nr1c1,r1c2,r1c3\nr2c1,r2c2,r2c3"));
return {
contents,
mime,
size: contents.length,
name
};
}
var file_generator_default = {
generateCsv,
generateDocx,
generateGif,
generateJpg,
generatePdf,
generateXlsx,
generatePng
};
export { file_generator_default as default };