This commit is contained in:
Tutur33
2023-11-24 22:35:41 +01:00
parent 3c0b507a93
commit 7644b2a0f7
45165 changed files with 4803356 additions and 3 deletions
+9
View File
@@ -0,0 +1,9 @@
# The MIT License
Copyright 2022 Harminder Virk, contributors
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the 'Software'), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+78
View File
@@ -0,0 +1,78 @@
# File generator
> Generate fake in-memory files for varying sizes
[![github-actions-image]][github-actions-url] [![npm-image]][npm-url] [![license-image]][license-url] [![typescript-image]][typescript-url]
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](https://gist.github.com/leommoore/f9e57ba2aa4bf197ebc5) 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.
```sh
npm i @poppinss/file-generator
# Yarn
yarn add @poppinss/file-generator
```
## Usage
Use the exported functions as follows.
```ts
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.
```ts
await generatePng('1mb', 'avatar.png')
```
## Usage with form-data
You can pass the generated content to an instance of form data as follows.
```ts
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.
[github-actions-image]: https://img.shields.io/github/workflow/status/poppinss/file-generator/test?style=for-the-badge
[github-actions-url]: https://github.com/poppinss/file-generator/actions/workflows/test.yml "github-actions"
[npm-image]: https://img.shields.io/npm/v/@poppinss/file-generator.svg?style=for-the-badge&logo=npm
[npm-url]: https://npmjs.org/package/@poppinss/file-generator "npm"
[license-image]: https://img.shields.io/npm/l/@poppinss/file-generator?color=blueviolet&style=for-the-badge
[license-url]: LICENSE.md "license"
[typescript-image]: https://img.shields.io/badge/Typescript-294E80.svg?style=for-the-badge&logo=typescript
[typescript-url]: "typescript"
+6
View File
@@ -0,0 +1,6 @@
export { generateDocx } from './src/files/docx';
export { generateGif } from './src/files/gif';
export { generateJpg } from './src/files/jpg';
export { generatePdf } from './src/files/pdf';
export { generatePng } from './src/files/png';
export { generateXlsx } from './src/files/xlsx';
+23
View File
@@ -0,0 +1,23 @@
"use strict";
/*
* @poppinss/file-generator
*
* (c) Poppinss
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
Object.defineProperty(exports, "__esModule", { value: true });
exports.generateXlsx = exports.generatePng = exports.generatePdf = exports.generateJpg = exports.generateGif = exports.generateDocx = void 0;
var docx_1 = require("./src/files/docx");
Object.defineProperty(exports, "generateDocx", { enumerable: true, get: function () { return docx_1.generateDocx; } });
var gif_1 = require("./src/files/gif");
Object.defineProperty(exports, "generateGif", { enumerable: true, get: function () { return gif_1.generateGif; } });
var jpg_1 = require("./src/files/jpg");
Object.defineProperty(exports, "generateJpg", { enumerable: true, get: function () { return jpg_1.generateJpg; } });
var pdf_1 = require("./src/files/pdf");
Object.defineProperty(exports, "generatePdf", { enumerable: true, get: function () { return pdf_1.generatePdf; } });
var png_1 = require("./src/files/png");
Object.defineProperty(exports, "generatePng", { enumerable: true, get: function () { return png_1.generatePng; } });
var xlsx_1 = require("./src/files/xlsx");
Object.defineProperty(exports, "generateXlsx", { enumerable: true, get: function () { return xlsx_1.generateXlsx; } });
Binary file not shown.
+13
View File
@@ -0,0 +1,13 @@
/// <reference types="node" />
/**
* Generates a fake docx file for the given file size.
*
* @example
* generate(1000 * 1000 * 2) // Generates a 2mb file
*/
export declare function generateDocx(fileSize: number | string, fileName?: string): Promise<{
contents: Buffer;
size: number;
name: string;
mime: string;
}>;
+28
View File
@@ -0,0 +1,28 @@
"use strict";
/*
* @poppinss/file-generator
*
* (c) Poppinss
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
Object.defineProperty(exports, "__esModule", { value: true });
exports.generateDocx = void 0;
const path_1 = require("path");
const crypto_1 = require("crypto");
const promises_1 = require("fs/promises");
const generate_1 = require("../generate");
/**
* Generates a fake docx file for the given file size.
*
* @example
* generate(1000 * 1000 * 2) // Generates a 2mb file
*/
async function generateDocx(fileSize, fileName) {
const mime = 'application/vnd.openxmlformats-officedocument.wordprocessingml.document';
const name = fileName || `${(0, crypto_1.randomUUID)()}.docx`;
const contents = (0, generate_1.generate)(fileSize, await (0, promises_1.readFile)((0, path_1.join)(__dirname, './fake.docx')));
return { contents, mime, size: contents.length, name };
}
exports.generateDocx = generateDocx;
+5
View File
@@ -0,0 +1,5 @@
/// <reference types="node" />
/**
* Generates a buffer for a given size with pre-filled contents
*/
export declare function generate(fileSize: string | number, contents: Buffer): Buffer;
+24
View File
@@ -0,0 +1,24 @@
"use strict";
/*
* @poppinss/file-generator
*
* (c) Poppinss
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.generate = void 0;
const bytes_1 = __importDefault(require("bytes"));
/**
* Generates a buffer for a given size with pre-filled contents
*/
function generate(fileSize, contents) {
const size = typeof fileSize === 'string' ? (0, bytes_1.default)(fileSize) : fileSize;
const contentsSize = contents.length;
return Buffer.alloc(size < contentsSize ? contentsSize : size, contents, 'binary');
}
exports.generate = generate;
Binary file not shown.

After

Width:  |  Height:  |  Size: 42 B

+13
View File
@@ -0,0 +1,13 @@
/// <reference types="node" />
/**
* Generates a fake gif file for the given file size.
*
* @example
* generate(1000 * 1000 * 2) // Generates a 2mb file
*/
export declare function generateGif(fileSize: number | string, fileName?: string): Promise<{
contents: Buffer;
size: number;
name: string;
mime: string;
}>;
+28
View File
@@ -0,0 +1,28 @@
"use strict";
/*
* @poppinss/file-generator
*
* (c) Poppinss
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
Object.defineProperty(exports, "__esModule", { value: true });
exports.generateGif = void 0;
const path_1 = require("path");
const crypto_1 = require("crypto");
const promises_1 = require("fs/promises");
const generate_1 = require("../generate");
/**
* Generates a fake gif file for the given file size.
*
* @example
* generate(1000 * 1000 * 2) // Generates a 2mb file
*/
async function generateGif(fileSize, fileName) {
const mime = 'image/gif';
const name = fileName || `${(0, crypto_1.randomUUID)()}.gif`;
const contents = (0, generate_1.generate)(fileSize, await (0, promises_1.readFile)((0, path_1.join)(__dirname, './fake.gif')));
return { contents, mime, size: contents.length, name };
}
exports.generateGif = generateGif;
Binary file not shown.

After

Width:  |  Height:  |  Size: 631 B

+13
View File
@@ -0,0 +1,13 @@
/// <reference types="node" />
/**
* Generates a fake jpg file for the given file size.
*
* @example
* generate(1000 * 1000 * 2) // Generates a 2mb file
*/
export declare function generateJpg(fileSize: number | string, fileName?: string): Promise<{
contents: Buffer;
size: number;
name: string;
mime: string;
}>;
+28
View File
@@ -0,0 +1,28 @@
"use strict";
/*
* @poppinss/file-generator
*
* (c) Poppinss
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
Object.defineProperty(exports, "__esModule", { value: true });
exports.generateJpg = void 0;
const path_1 = require("path");
const crypto_1 = require("crypto");
const promises_1 = require("fs/promises");
const generate_1 = require("../generate");
/**
* Generates a fake jpg file for the given file size.
*
* @example
* generate(1000 * 1000 * 2) // Generates a 2mb file
*/
async function generateJpg(fileSize, fileName) {
const mime = 'image/jpeg';
const name = fileName || `${(0, crypto_1.randomUUID)()}.jpg`;
const contents = (0, generate_1.generate)(fileSize, await (0, promises_1.readFile)((0, path_1.join)(__dirname, './fake.jpg')));
return { contents, mime, size: contents.length, name };
}
exports.generateJpg = generateJpg;
Binary file not shown.
+13
View File
@@ -0,0 +1,13 @@
/// <reference types="node" />
/**
* Generates a fake pdf file for the given file size.
*
* @example
* generate(1000 * 1000 * 2) // Generates a 2mb file
*/
export declare function generatePdf(fileSize: number | string, fileName?: string): Promise<{
contents: Buffer;
size: number;
name: string;
mime: string;
}>;
+28
View File
@@ -0,0 +1,28 @@
"use strict";
/*
* @poppinss/file-generator
*
* (c) Poppinss
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
Object.defineProperty(exports, "__esModule", { value: true });
exports.generatePdf = void 0;
const path_1 = require("path");
const crypto_1 = require("crypto");
const promises_1 = require("fs/promises");
const generate_1 = require("../generate");
/**
* Generates a fake pdf file for the given file size.
*
* @example
* generate(1000 * 1000 * 2) // Generates a 2mb file
*/
async function generatePdf(fileSize, fileName) {
const mime = 'application/pdf';
const name = fileName || `${(0, crypto_1.randomUUID)()}.pdf`;
const contents = (0, generate_1.generate)(fileSize, await (0, promises_1.readFile)((0, path_1.join)(__dirname, './fake.pdf')));
return { contents, mime, size: contents.length, name };
}
exports.generatePdf = generatePdf;
Binary file not shown.

After

Width:  |  Height:  |  Size: 68 B

+13
View File
@@ -0,0 +1,13 @@
/// <reference types="node" />
/**
* Generates a fake png file for the given file size.
*
* @example
* generate(1000 * 1000 * 2) // Generates a 2mb file
*/
export declare function generatePng(fileSize: number | string, fileName?: string): Promise<{
contents: Buffer;
size: number;
name: string;
mime: string;
}>;
+28
View File
@@ -0,0 +1,28 @@
"use strict";
/*
* @poppinss/file-generator
*
* (c) Poppinss
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
Object.defineProperty(exports, "__esModule", { value: true });
exports.generatePng = void 0;
const path_1 = require("path");
const crypto_1 = require("crypto");
const promises_1 = require("fs/promises");
const generate_1 = require("../generate");
/**
* Generates a fake png file for the given file size.
*
* @example
* generate(1000 * 1000 * 2) // Generates a 2mb file
*/
async function generatePng(fileSize, fileName) {
const mime = 'image/png';
const name = fileName || `${(0, crypto_1.randomUUID)()}.png`;
const contents = (0, generate_1.generate)(fileSize, await (0, promises_1.readFile)((0, path_1.join)(__dirname, './fake.png')));
return { contents, mime, size: contents.length, name };
}
exports.generatePng = generatePng;
Binary file not shown.
+13
View File
@@ -0,0 +1,13 @@
/// <reference types="node" />
/**
* Generates a fake xlxs file for the given file size.
*
* @example
* generate(1000 * 1000 * 2) // Generates a 2mb file
*/
export declare function generateXlsx(fileSize: number | string, fileName?: string): Promise<{
contents: Buffer;
size: number;
name: string;
mime: string;
}>;
+28
View File
@@ -0,0 +1,28 @@
"use strict";
/*
* @poppinss/file-generator
*
* (c) Poppinss
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
Object.defineProperty(exports, "__esModule", { value: true });
exports.generateXlsx = void 0;
const path_1 = require("path");
const crypto_1 = require("crypto");
const promises_1 = require("fs/promises");
const generate_1 = require("../generate");
/**
* Generates a fake xlxs file for the given file size.
*
* @example
* generate(1000 * 1000 * 2) // Generates a 2mb file
*/
async function generateXlsx(fileSize, fileName) {
const mime = 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet';
const name = fileName || `${(0, crypto_1.randomUUID)()}.xlsx`;
const contents = (0, generate_1.generate)(fileSize, await (0, promises_1.readFile)((0, path_1.join)(__dirname, './fake.xlsx')));
return { contents, mime, size: contents.length, name };
}
exports.generateXlsx = generateXlsx;
+135
View File
@@ -0,0 +1,135 @@
{
"name": "@poppinss/file-generator",
"version": "1.0.2",
"description": "Generate in-memory fake files for custom size",
"files": [
"build/src",
"build/index.d.ts",
"build/index.js"
],
"types": "./build/index.d.ts",
"exports": {
".": {
"types": "./build/index.d.ts",
"require": "./build/index.js"
}
},
"scripts": {
"mrm": "mrm --preset=@adonisjs/mrm-preset",
"pretest": "npm run lint",
"test": "node -r @adonisjs/require-ts/build/register bin/test.ts",
"clean": "del-cli build",
"copy:images": "copyfiles 'src/files/**/*{.png,.gif,.jpg,.pdf,.docx,.xlsx}' build",
"compile": "npm run lint && npm run clean && tsc && npm run copy:images",
"build": "npm run compile",
"prepublishOnly": "npm run build",
"lint": "eslint . --ext=.ts",
"format": "prettier --write .",
"commit": "git-cz",
"release": "np --message=\"chore(release): %s\"",
"version": "npm run build",
"sync-labels": "github-label-sync --labels ./node_modules/@adonisjs/mrm-preset/gh-labels.json poppinss/file-generator"
},
"keywords": [
"file-generator",
"fake-file",
"faker-file"
],
"author": "virk,poppinss",
"license": "MIT",
"devDependencies": {
"@adonisjs/mrm-preset": "^5.0.3",
"@adonisjs/require-ts": "^2.0.11",
"@japa/assert": "^1.3.4",
"@japa/run-failed-tests": "^1.0.7",
"@japa/runner": "^2.0.7",
"@japa/spec-reporter": "^1.1.12",
"@types/node": "^17.0.23",
"commitizen": "^4.2.4",
"copyfiles": "^2.4.1",
"cz-conventional-changelog": "^3.3.0",
"del-cli": "^4.0.1",
"eslint": "^8.13.0",
"eslint-config-prettier": "^8.5.0",
"eslint-plugin-adonis": "^2.1.0",
"eslint-plugin-prettier": "^4.0.0",
"file-type": "^17.1.1",
"github-label-sync": "^2.2.0",
"husky": "^7.0.4",
"mrm": "^4.0.0",
"np": "^7.6.1",
"prettier": "^2.6.2",
"typescript": "^4.6.3"
},
"mrmConfig": {
"core": false,
"license": "MIT",
"services": [
"github-actions"
],
"minNodeVersion": "16.13.1",
"probotApps": [
"stale",
"lock"
],
"runGhActionsOnWindows": true
},
"eslintConfig": {
"extends": [
"plugin:adonis/typescriptPackage",
"prettier"
],
"plugins": [
"prettier"
],
"rules": {
"prettier/prettier": [
"error",
{
"endOfLine": "auto"
}
]
}
},
"eslintIgnore": [
"build"
],
"prettier": {
"trailingComma": "es5",
"semi": false,
"singleQuote": true,
"useTabs": false,
"quoteProps": "consistent",
"bracketSpacing": true,
"arrowParens": "always",
"printWidth": 100
},
"config": {
"commitizen": {
"path": "cz-conventional-changelog"
}
},
"publishConfig": {
"access": "public",
"tag": "latest"
},
"np": {
"contents": ".",
"anyBranch": false
},
"dependencies": {
"bytes": "^3.1.2"
},
"main": "index.js",
"directories": {
"test": "tests"
},
"repository": {
"type": "git",
"url": "git+https://github.com/poppinss/file-generator.git"
},
"bugs": {
"url": "https://github.com/poppinss/file-generator/issues"
},
"homepage": "https://github.com/poppinss/file-generator#readme"
}