@poppinss/file-generator
Version:
Generate in-memory fake files for custom size
124 lines (114 loc) • 4.75 kB
JavaScript
// src/files/docx/generate.ts
import { join } from "node:path";
import { randomUUID } from "node:crypto";
import { readFile } from "node:fs/promises";
// src/files/utilities.ts
import bytes from "bytes";
import { fileURLToPath } from "node:url";
import { dirname as pathDirname } from "node:path";
var E_INVALID_FILE_SIZE_EXPRESSION = (fileSize) => {
const error = new Error(
`Invalid fileSize value "${fileSize}". Expected value to be a number representing bytes or a string expression`
);
Object.defineProperty(error, "hint", {
value: "Check https://www.npmjs.com/package/bytes package to view supported string expressions"
});
return error;
};
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(url) {
return pathDirname(fileURLToPath(url));
}
// src/files/docx/generate.ts
async function generateDocx(fileSize, fileName) {
const mime = "application/vnd.openxmlformats-officedocument.wordprocessingml.document";
const name = fileName || `${randomUUID()}.docx`;
const initialBytes = await readFile(join(dirname(import.meta.url), "./fake.docx"));
const contents = toBuffer(fileSize, initialBytes);
return { contents, mime, size: contents.length, name };
}
// src/files/gif/generate.ts
import { join as join2 } from "node:path";
import { randomUUID as randomUUID2 } from "node:crypto";
import { readFile as readFile2 } from "node:fs/promises";
async function generateGif(fileSize, fileName) {
const mime = "image/gif";
const name = fileName || `${randomUUID2()}.gif`;
const initialBytes = await readFile2(join2(dirname(import.meta.url), "./fake.gif"));
const contents = toBuffer(fileSize, initialBytes);
return { contents, mime, size: contents.length, name };
}
// src/files/jpg/generate.ts
import { join as join3 } from "node:path";
import { randomUUID as randomUUID3 } from "node:crypto";
import { readFile as readFile3 } from "node:fs/promises";
async function generateJpg(fileSize, fileName) {
const mime = "image/jpeg";
const name = fileName || `${randomUUID3()}.jpg`;
const initialBytes = await readFile3(join3(dirname(import.meta.url), "./fake.jpg"));
const contents = toBuffer(fileSize, initialBytes);
return { contents, mime, size: contents.length, name };
}
// src/files/pdf/generate.ts
import { join as join4 } from "node:path";
import { randomUUID as randomUUID4 } from "node:crypto";
import { readFile as readFile4 } from "node:fs/promises";
async function generatePdf(fileSize, fileName) {
const mime = "application/pdf";
const name = fileName || `${randomUUID4()}.pdf`;
const initialBytes = await readFile4(join4(dirname(import.meta.url), "./fake.pdf"));
const contents = toBuffer(fileSize, initialBytes);
return { contents, mime, size: contents.length, name };
}
// src/files/png/generate.ts
import { join as join5 } from "node:path";
import { randomUUID as randomUUID5 } from "node:crypto";
import { readFile as readFile5 } from "node:fs/promises";
async function generatePng(fileSize, fileName) {
const mime = "image/png";
const name = fileName || `${randomUUID5()}.png`;
const initialBytes = await readFile5(join5(dirname(import.meta.url), "./fake.png"));
const contents = toBuffer(fileSize, initialBytes);
return { contents, mime, size: contents.length, name };
}
// src/files/xlsx/generate.ts
import { join as join6 } from "node:path";
import { randomUUID as randomUUID6 } from "node:crypto";
import { readFile as readFile6 } from "node:fs/promises";
async function generateXlsx(fileSize, fileName) {
const mime = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
const name = fileName || `${randomUUID6()}.xlsx`;
const initialBytes = await readFile6(join6(dirname(import.meta.url), "./fake.xlsx"));
const contents = toBuffer(fileSize, initialBytes);
return { contents, mime, size: contents.length, name };
}
// src/files/csv/generate.ts
import { randomUUID as randomUUID7 } from "node:crypto";
async function generateCsv(fileSize, fileName) {
const mime = "text/csv";
const name = fileName ?? `${randomUUID7()}.csv`;
const initialBytes = Buffer.from("h1,h2,h3\nr1c1,r1c2,r1c3\nr2c1,r2c2,r2c3");
const contents = toBuffer(fileSize, initialBytes);
return { contents, mime, size: contents.length, name };
}
// index.ts
var fileGenerator = {
generateCsv,
generateDocx,
generateGif,
generateJpg,
generatePdf,
generateXlsx,
generatePng
};
var index_default = fileGenerator;
export {
index_default as default
};