Generate in-memory fake files with custom size

Overview

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.
You might also like...

This experimental library patches the global custom elements registry to allow re-defining or reload a custom element.

Redefine Custom Elements This experimental library patches the global custom elements registry to allow re-defining a custom element. Based on the spe

Dec 11, 2022

Ready to manipulate partitions file? Create a custom partition, apply custom security system, hide the partition and share your hidden data on the www

Ready to manipulate partitions file? Create a custom partition, apply custom security system, hide the partition and share your hidden data on the www

Paranoia 💊 Ready to manipulate partitions file? Create a custom partition, apply custom security system, hide the partition and share your hidden dat

Dec 29, 2022

Custom Gutenburg blocks to add custom functionalities to your WordPress site. Brought to you by Arif Khan with 3

Custom Gutenburg blocks to add custom functionalities to your WordPress site. Brought to you by Arif Khan with <3

Gutpress(WordPress Gutenburg Block Plugin) Custom Gutenburg blocks to add custom functionalities to your WordPress site. Brought to you by Arif Khan w

Nov 23, 2022

Custom navigations for Solid written in Typescript. Implement custom page transition logic and ✨ animations ✨

solid-custom-navigation Get, Set, Go! Custom navigations for Solid, written in Typescript. Implement custom page transition logic and ✨ animations ✨ .

Nov 27, 2022

Create Bootstrap 5 Modal Box using JavaScript with custom title, description, button labels and custom YES button callback

Create Bootstrap 5 Modal Box using JavaScript with custom title, description, button labels and custom YES button callback

Dynamic BS5 Modal Box Create Bootstrap 5 Modal Box using JavaScript with custom title, description, button labels and custom YES button callback Insta

Oct 23, 2022

Base-mock-api - Repo to storage my fake api's to use in my 2022 projects.

Base Mock API's Project made 100% with JavaScript, with the objective of creating endpoints to use in projects. Prerequisites Before you begin, ensure

Nov 20, 2022

Mixed Messages is a simple Node.js application, that will print a randomized fake fact to the terminal each time it is ran.

Mixed Messages - Fake Fact Generator Mixed Messages is a simple Node.js application, That will print a randomized fake fact to the terminal each time

Jan 10, 2022

A JavaScript library built on top of the Faker.JS library. It generates massive amounts of fake data in the browser and node.js.

Blaver - generate massive amounts of fake data in the browser and node.js Blaver is a JavaScript library built on top of the Faker.JS library. It gene

Dec 30, 2022

All the Fake Data for All Your Real Needs 🙂

All the Fake Data for All Your Real Needs 🙂

All the Fake Data for All Your Real Needs 🙂 Run it on Stackblitz Installation npm i @ngneat/falso yarn add @ngneat/falso Methods randT(arr: T[]):

Dec 29, 2022
Comments
  • Cannot find module '@poppinss/file-generator'

    Cannot find module '@poppinss/file-generator'

    Description

    I received error on latest package version. I develop some package for adonis and this package are used as fold dependency. Cannot find module '@poppinss/file-generator' from 'node_modules/@adonisjs/http server/node_modules/@poppinss/utils/build/src/Helpers/index.js'

    I think this issue related to this https://github.com/poppinss/file-generator/commit/58ef657490d97393da1fdfa7b2b9901c860fea7f All works perfectly with version 1.0.1

    Package version

    1.0.2

    Node.js and npm version

    Reproduced on:

    • Node 14.18.1 & NPM 6.14.15
    • Node 16.14.2 & NPM 8.5.0
    opened by VladyslavParashchenko 3
  • Feature Request - Add csv support

    Feature Request - Add csv support

    Description

    • text/csv mimetype should do.
    • I think csv files don't have magic bytes in it, it's just a text file.
    • Use case:
      • Faking a csv upload test in an adonis project

    Relevant information

    • I can try to help if this request is accepted
    Type: Enhancement 
    opened by brkn 1
Releases(v2.0.1-0)
  • v2.0.1-0(Nov 1, 2022)

    The release contains a breaking change, the main entrypoint uses default export now

    // Earlier
    import {  generatePng } from '@poppinss/file-generator'
    generatePng()
    
    // Now
    import fileGenerator from '@poppinss/file-generator'
    fileGenerator.generatePng()
    

    Commits

    • refactor: use default export for the package entrypoint 168d1ab
    • chore: track and improve coverage b7e5a95
    • refactor: update meta files 7e6927a

    Full Changelog: https://github.com/poppinss/file-generator/compare/v2.0.0-0...v2.0.1-0

    Source code(tar.gz)
    Source code(zip)
  • v2.0.0-0(Sep 25, 2022)

    Breaking change. The package API remains the same, except it is ESM only now.

    • chore: disable usage of package-lock file de219ac
    • docs(README): document all methods b7e54c6
    • docs(README): tweak some sentences 0b322f4
    • test: fix import paths to work on windows e6a9a1a
    • refactor: csv file generate method 5d11679
    • refactor: move to ESM f70587e
    • feat: Add csv faking (#2) 7b52efc

    PRs

    • feat: Add csv faking by @brkn in https://github.com/poppinss/file-generator/pull/2

    New Contributors

    • @brkn made their first contribution in https://github.com/poppinss/file-generator/pull/2

    Full Changelog: https://github.com/poppinss/file-generator/compare/v1.0.2...v2.0.0-0

    Source code(tar.gz)
    Source code(zip)
  • v1.0.2(Apr 10, 2022)

    • fix: register types 58ef657
    • chore: update dependencies e0f52dc
    • chore: add repo info to package.json file 0396fa9

    https://github.com/poppinss/file-generator/compare/v1.0.1...v1.0.2

    Source code(tar.gz)
    Source code(zip)
  • v1.0.1(Apr 2, 2022)

    Commits:

    • chore: do not run tests from the build folder 5e01627
    • fix: copy non source files to the build folder 8493f50

    Commit Range: v1.0.0...main

    Source code(tar.gz)
    Source code(zip)
  • v1.0.0(Apr 1, 2022)

    Commits:

    • chore(package): add publishConfig 6425415
    • docs: small fixes 91912f0

    Commit Range: 73674befd51f220b276ac659b5d1d414160830d7...main

    Source code(tar.gz)
    Source code(zip)
Owner
Poppinss
Small focused modules for Node.js
Poppinss
Kuldeep 2 Jun 21, 2022
faker.js - generate massive amounts of fake data in the browser and node.js

faker.js - generate massive amounts of fake data in the browser and node.js

Wanderson Camargo 2 Jan 7, 2022
Generate massive amounts of fake data in the browser and node.js

Faker Generate massive amounts of fake data in the browser and node.js. FAQ - What happened to the original faker.js? This project was originally crea

faker-js 8.4k Jan 2, 2023
Generate massive amounts of fake data in the browser and node.js

Faker Generate massive amounts of fake data in the Browser and Node.js. Installation Please replace your faker dependency with @faker-js/faker. This i

faker-js 8.4k Jan 4, 2023
Generate convincing fake tweet images

Fake Tweet Generator Description With the Fake Tweet Generator tool you can create convincing fake tweet images. This tool is completely customizable

Shashikanth Reddy 29 Dec 26, 2022
A repository to generate the fake json data from protobuf.

Mock Protobuf A command-line tool to mock protobuf! Table of Contents Install Usage Generate Mock Data Mock Server Mock Server Data Filter Include Fil

Jasonkay 9 Dec 7, 2022
一个运行在浏览器内的提词器,可翻页,可变速,可自定义字体、大小、颜色等多种选项 A teleprompter in web browser, page down, speed up , custom font family/size/color and more.

Introduction 一个运行在浏览器内的提词器,可翻页,可变速,可自定义字体、大小、颜色等多种选项 A teleprompter in web browser, page down, speed up , custom font family/size/color and more. inst

Daniel Liu 19 Aug 17, 2021
Generate smooth, consistent and always-sexy box-shadows, no matter the size, ideal for design token generation.

smooth-shadow Generate smooth, consistent and always-sexy box-shadows, no matter the size, ideal for design token generation. Demo As Tobias already p

Thomas Strobl 4 Oct 15, 2022
🌸 A cli can automatically generate files from Excel files.

unxlsx A cli can automatically generate files from Excel files. Why We often need to export some information from XLSX to generate our files, such as

Frozen FIsh 24 Aug 22, 2022
logseq custom.js and custom.css utilities : resize query table columns, hide namespaces...

logseq-custom-files custom.js and custom.css utilities for Logseq. current version v20220331 query table view : add handles on the query table headers

null 44 Dec 7, 2022