Security News
Combatting Alert Fatigue by Prioritizing Malicious Intent
In 2023, data breaches surged 78% from zero-day and supply chain attacks, but developers are still buried under alerts that are unable to prevent these threats.
extract-files
Advanced tools
Clones a value, recursively extracting File, Blob and ReactNativeFile instances with their object paths, replacing them with null. FileList instances are treated as File instance arrays.
The extract-files package is designed to extract files from a JavaScript object tree so they can be uploaded via a multipart request. It is commonly used in applications that need to handle file uploads, especially in the context of GraphQL operations.
Extract files from an object
This feature allows you to extract files from an object, which is useful when preparing files for upload. The `extractFiles` function takes an object and a path to traverse within the object to find files. It returns an object with the extracted files and a clone of the original object with files replaced by null.
{"operation": "const { extractFiles } = require('extract-files');\nconst file = new File(['content'], 'file.txt', { type: 'text/plain' });\nconst operation = { variables: { file } };\nconst { files, clone } = extractFiles(operation, 'variables');"}
Support for FileList and Map objects
The package can handle `FileList` objects, which are typically obtained from file input elements, and `Map` objects. It can extract files from these complex structures, making it versatile for various file upload scenarios.
{"operation": "const { extractFiles } = require('extract-files');\nconst fileList = document.querySelector('input[type=file]').files;\nconst operation = { variables: { files: fileList } };\nconst { files, clone } = extractFiles(operation, 'variables');"}
The form-data package allows you to create `multipart/form-data` streams to submit files and values via HTTP. It can be used to simulate a form submission with file uploads, similar to extract-files, but it is more focused on constructing the form data itself rather than extracting files from an existing object structure.
Busboy is a Node.js module for parsing incoming HTML form data, including file uploads. It differs from extract-files in that it is used on the server side to process file uploads, whereas extract-files is typically used on the client side to prepare files for upload.
Multer is a Node.js middleware for handling `multipart/form-data`, primarily used for uploading files. It is similar to busboy but is designed to be used with Express applications. Unlike extract-files, multer is not about extracting files from an object but rather about handling file uploads on the server side.
Clones a value, recursively extracting File
, Blob
and ReactNativeFile
instances with their object paths, replacing them with null
. FileList
instances are treated as File
instance arrays.
Used by GraphQL multipart request spec client implementations such as graphql-react
and apollo-upload-client
.
To install with npm, run:
npm install extract-files
See the documentation for the function extractFiles
to get started.
^12.20 || >= 14.13
> 0.5%, not OperaMini all, not IE > 0, not dead
Used to mark a React Native File
substitute in an object tree for extractFiles
. It’s too risky to assume all objects with uri
, type
and name
properties are files to extract.
Parameter | Type | Description |
---|---|---|
file | ReactNativeFileSubstitute | A React Native File substitute. |
Ways to import
.
import { ReactNativeFile } from 'extract-files';
import ReactNativeFile from 'extract-files/public/ReactNativeFile.js';
Ways to require
.
const { ReactNativeFile } = require('extract-files');
const ReactNativeFile = require('extract-files/public/ReactNativeFile.js');
An extractable file in React Native.
const file = new ReactNativeFile({
uri: uriFromCameraRoll,
name: 'a.jpg',
type: 'image/jpeg',
});
Clones a value, recursively extracting File
, Blob
and ReactNativeFile
instances with their object paths, replacing them with null
. FileList
instances are treated as File
instance arrays.
Parameter | Type | Description |
---|---|---|
value | * | Value (typically an object tree) to extract files from. |
path | ObjectPath? = '' | Prefix for object paths for extracted files. |
isExtractableFile | ExtractableFileMatcher? = isExtractableFile | The function used to identify extractable files. |
Returns: ExtractFilesResult — Result.
Ways to import
.
import { extractFiles } from 'extract-files';
import extractFiles from 'extract-files/public/extractFiles.js';
Ways to require
.
const { extractFiles } = require('extract-files');
const extractFiles = require('extract-files/public/extractFiles.js');
Extract files from an object.
For the following:
const file1 = new File(['1'], '1.txt', { type: 'text/plain' }); const file2 = new File(['2'], '2.txt', { type: 'text/plain' }); const value = { a: file1, b: [file1, file2], }; const { clone, files } = extractFiles(value, 'prefix');
value
remains the same.
clone
is:
{ "a": null, "b": [null, null] }
files
is aMap
instance containing:
Key Value file1
['prefix.a', 'prefix.b.0']
file2
['prefix.b.1']
Checks if a value is an extractable file.
Type: ExtractableFileMatcher
Parameter | Type | Description |
---|---|---|
value | * | Value to check. |
Returns: boolean — Is the value an extractable file.
Ways to import
.
import { isExtractableFile } from 'extract-files';
import isExtractableFile from 'extract-files/public/isExtractableFile.js';
Ways to require
.
const { isExtractableFile } = require('extract-files');
const isExtractableFile = require('extract-files/public/isExtractableFile.js');
An extractable file.
Type: File | Blob | ReactNativeFile
A function that checks if a value is an extractable file.
Type: Function
Parameter | Type | Description |
---|---|---|
value | * | Value to check. |
Returns: boolean — Is the value an extractable file.
isExtractableFile
is the default extractable file matcher.How to check for the default exactable files, as well as a custom type of file.
import isExtractableFile from 'extract-files/public/isExtractableFile.js';
const isExtractableFileEnhanced = (value) =>
isExtractableFile(value) ||
(typeof CustomFile !== 'undefined' && value instanceof CustomFile);
What extractFiles
returns.
Type: object
Property | Type | Description |
---|---|---|
clone | * | Clone of the original input value with files recursively replaced with null . |
files | Map<ExtractableFile, Array<ObjectPath>> | Extracted files and their locations within the original value. |
String notation for the path to a node in an object tree.
Type: string
Object path is property a
, array index 0
, object property b
.
a.0.b
A React Native File
substitute for when using FormData
.
Type: object
Property | Type | Description |
---|---|---|
uri | string | Filesystem path. |
name | string? | File name. |
type | string? | File content type. |
A camera roll file.
const fileSubstitute = {
uri: uriFromCameraRoll,
name: 'a.jpg',
type: 'image/jpeg',
};
11.0.0
extractFiles
now deeply clones an input value containing multiple references of an object or array with a mirrored reference structure instead of creating multiple objects or arrays. This change shouldn’t affect typical JSON.stringify
use with cloned values.extractFiles
now uses for…of
to iterate FileList
instances.main
field path.extractFiles
now correctly handles circular references within the input value by recreating the circular references in the returned clone instead of infinitely recursing to the point of a Maximum call stack size exceeded
error, fixing #14.extractFiles
tests to use Object.freeze
with input objects and arrays to ensure input isn’t mutated.FAQs
A function to recursively extract files and their object paths within a value, replacing them with null in a deep clone without mutating the original value. FileList instances are treated as File instance arrays. Files are typically File and Blob instance
The npm package extract-files receives a total of 4,382,925 weekly downloads. As such, extract-files popularity was classified as popular.
We found that extract-files demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
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.
Security News
In 2023, data breaches surged 78% from zero-day and supply chain attacks, but developers are still buried under alerts that are unable to prevent these threats.
Security News
Solo open source maintainers face burnout and security challenges, with 60% unpaid and 60% considering quitting.
Security News
License exceptions modify the terms of open source licenses, impacting how software can be used, modified, and distributed. Developers should be aware of the legal implications of these exceptions.