npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@poppinss/file-generator

v2.1.3

Published

Generate in-memory fake files for custom size

Downloads

96,741

Readme

File generator

Generate fake in-memory files for varying sizes

github-actions-image npm-image license-image typescript-image

Note: This package is ESM only

This package allows you generate fake in-memory files of varying sizes. The generated file can be used during testing to test the file uploads functionality of your Node server.

  • Support for docx, csv, xlsx, pdf, png, jpg, and gif files.
  • Passes the magic number file validation.
  • The file contents are kept inside memory as a buffer. No files are written to the disk.

Installation

Install the package from the npm registry as follows.

npm i @poppinss/file-generator

# Yarn
yarn add @poppinss/file-generator

Usage

Use the exported functions as follows.

import fileGenerator from '@poppinss/file-generator'

const {
  contents,
  size,
  mime,
  name
} = await fileGenerator.generatePng('1mb')
  • contents is a buffer.
  • size is the size of the file in bytes.
  • mime is the mime type for the generated file.
  • name is a randomly assigned unique name to the file.

You can also define a custom file name as the second argument.

await fileGenerator.generatePng('1mb', 'avatar.png')

Usage with form-data

You can pass the generated content to an instance of form data as follows.

import FormData from 'form-data'

const form = new FormData()
const file = await fileGenerator.generatePng('1mb')

form.append('avatar', file.contents, {
  filename: file.name,
  contentType: file.mime,
  knownLength: file.size,
})

Points to note

  • Only the first few bytes of the files are valid and rest of the bytes are empty. Therefore, further processing of the files will not work. For example: If you open the PDF file to read its content on the server, then using this package is not the right choice.
  • Every file type has minimum bytes and you cannot generate files smaller than that. This is done to keep the initial bytes valid and them pass the standard validation rules.

Available methods

Following are the available methods to generate different files.

  • generateDocx - Generate a Microsoft word doc file (passes file-type validation).
  • generateGif - Generate a gif file (passes file-type validation).
  • generateJpg - Generate a jpeg file (passes file-type validation).
  • generatePdf - Generate a pdf file (passes file-type validation).
  • generatePng - Generate a png file (passes file-type validation).
  • generateXlsx - Generate a Microsoft excel spreadsheet (passes file-type validation).
  • generateCsv - Generate a CSV file.