Code Monkey home page Code Monkey logo

file-generator's Introduction

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.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.