Socket
Socket
Sign inDemoInstall

@poppinss/file-generator

Package Overview
Dependencies
Maintainers
1
Versions
13
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@poppinss/file-generator

Generate in-memory fake files for custom size


Version published
Weekly downloads
17K
decreased by-9.61%
Maintainers
1
Weekly downloads
 
Created
Source

File generator

Generate fake in-memory files for varying sizes

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

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

  • Support for docx, xlsx, pdf, png, jpg, and gif files.
  • Passes the magic number file validation.
  • The file contents is kept in-memory 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 { generatePng } from '@poppinss/file-generator'

const {
  contents,
  size,
  mime,
  name
} = await 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 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 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.

Keywords

FAQs

Package last updated on 10 Apr 2022

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc