Big News: Socket raises $60M Series C at a $1B valuation to secure software supply chains for AI-driven development.Announcement
Sign In

@multipart/form-data

Package Overview
Dependencies
Maintainers
1
Versions
2
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@multipart/form-data

Multipart/Form-Data And File Upload Middleware For Koa Written In ES6 And Optimised With JavaScript Compiler.

latest
Source
npmnpm
Version
1.0.1
Version published
Maintainers
1
Created
Source

@multipart/form-data

npm version

@multipart/form-data is Multipart/Form-Data And File Upload Middleware For Koa Written In ES6 And Optimised With JavaScript Compiler.

Originally, this was a Multer fork, however it was rewritten specifically for Koa2, and the interfaces were updated to be async rather than callbacks. Differences:

  • When the file size limit is reached, the next middleware is called, rather than waiting to drain the request stream. This can result in the client-side EPIPE (connection reset) errors when sending files larger than allowed. But ideally, Node.JS applications should be run behind a proxy such as NginX to limit the upload size.
  • Removes the unnecessary typeis dependency that includes the mime-type database, just checks the Content-Type to start with multipart/form-data.
  • Compiled with Google Closure Compiler and has just 1 dependency (text-decoding) to decode non-utf8 fields (e.g., when a form submitted had the accept-charset attribute).
yarn add @multipart/form-data

Table Of Contents

API

The package is available by importing its default and named functions:

import FormData, {
  diskStorage, memoryStorage, FormDataError,
} from '@multipart/form-data'

class FormData

This class is used to create middleware according to the required file upload strategy.

FormData: An instance to create middleware.

NameType & Description
constructornew (options?: !FormDataConfig) => FormData
Creates a new form-data instance.
single(name: string) => !_goa.Middleware
Accept a single file.
array(name: string, maxFiles: string) => !_goa.Middleware
Accept multiple files.
fields(fields: !Array<FormDataField>) => !_goa.Middleware
Accept files according to the configured fields.
none() => !_goa.Middleware
Do not accept files, only fields.
any() => !_goa.Middleware
Accept any fields and files.

Creates a new instance according to the config. It is later used to access the middleware functions described below.

FormDataConfig: The configuration for the instance.

NameTypeDescriptionDefault
deststringThe directory where to store the files using the DiskStorage. If not specified, files will be saved in the system's temp directory (os.tmpdir()).-
storageFormDataStorageEngineAn instance of a custom storage engine.-
fileFilterFormDataFileFilterThe file filter.-
limits_goa.BusBoyLimitsThe limits of the uploaded data.-
preservePathbooleanWhether to keep the full path of files instead of just the base name.false

single(fieldname): Accept a single file.

import Multipart from '@multipart/form-data'
import Goa from '@goa/koa'

const app = new Goa()
const multipart = new Multipart({
  dest: 'temp',
})
const middleware = multipart.single('file')
app.use(middleware)
app.use((ctx) => {
  console.log('Fields: %O', ctx.req.body)
  delete ctx.req.file.stream
  console.log('File: %O', ctx.req.file)
})
Fields: { hello: 'world', name: 'multipart' }
File: { fieldname: 'file',
  originalname: 'test.txt',
  encoding: '7bit',
  mimetype: 'application/octet-stream',
  destination: 'temp',
  filename: 'afb49cada5f721d7fa8337f072d03ec5',
  path: 'temp/afb49cada5f721d7fa8337f072d03ec5',
  size: 12 }

array(fieldname, maxCount): Accept multiple files under the same field name.

import Multipart from '@multipart/form-data'
import Goa from '@goa/koa'

const app = new Goa()
const multipart = new Multipart({
  dest: 'temp',
  preservePath: true,
})
const middleware = multipart.array('file', 2)
app.use(middleware)
app.use((ctx) => {
  log('Fields', ctx.req.body)
  log('Files', ctx.req.files)
})
Fields: { hello: 'world', name: 'multipart' }
Files: [ { fieldname: 'file',
    originalname: 'test/fixture/test.txt',
    encoding: '7bit',
    mimetype: 'application/octet-stream',
    destination: 'temp',
    filename: '0fa202db40',
    path: 'temp/0fa202db40',
    size: 12 },
  { fieldname: 'file',
    originalname: 'test/fixture/test.txt',
    encoding: '7bit',
    mimetype: 'application/octet-stream',
    destination: 'temp',
    filename: '149e4b08d6',
    path: 'temp/149e4b08d6',
    size: 12 } ]

fields(Array<FormDataField>): Accept files according to the configured fields and place them in a hashmap.

Click to show the FormDataField interface.

FormDataField: The item to use in the .fields method.

NameTypeDescription
name*stringThe name of the field.
maxCountnumberThe maximum count of the field.
import Multipart from '@multipart/form-data'
import Goa from '@goa/koa'

const app = new Goa()
const multipart = new Multipart({
  dest: 'temp',
})
const middleware = multipart.fields([
  { name: 'file', maxCount: 2 },
  { name: 'picture', maxCount: 1 },
])
app.use(middleware)
app.use((ctx) => {
  log('Fields', ctx.req.body)
  log('Files', ctx.req.files)
})
Fields: { hello: 'world', name: 'multipart' }
Files: { file: 
   [ { fieldname: 'file',
       originalname: 'test.txt',
       encoding: '7bit',
       mimetype: 'application/octet-stream',
       destination: 'temp',
       filename: '13093f0764',
       path: 'temp/13093f0764',
       size: 12 },
     { fieldname: 'file',
       originalname: 'test.txt',
       encoding: '7bit',
       mimetype: 'application/octet-stream',
       destination: 'temp',
       filename: '22e2e6e6f7',
       path: 'temp/22e2e6e6f7',
       size: 12 } ],
  picture: 
   [ { fieldname: 'picture',
       originalname: 'large.jpg',
       encoding: '7bit',
       mimetype: 'application/octet-stream',
       destination: 'temp',
       filename: '352a1aea6a',
       path: 'temp/352a1aea6a',
       size: 1592548 } ] }

none(): Do not accept files, only fields.

import Multipart from '@multipart/form-data'
import Goa from '@goa/koa'

const app = new Goa()
const multipart = new Multipart({
  dest: 'temp',
})
const middleware = multipart.none()
app.use(middleware)
app.use((ctx) => {
  log('Fields', ctx.req.body)
  log('Files', ctx.req.files)
})
Fields: { hello: 'world', name: 'multipart' }
Files: undefined

any(): Accept all files and fields.

import Multipart from '@multipart/form-data'
import Goa from '@goa/koa'

const app = new Goa()
const multipart = new Multipart({
  dest: 'temp',
})
const middleware = multipart.any()
app.use(middleware)
app.use((ctx) => {
  log('Fields', ctx.req.body)
  log('Files', ctx.req.files)
})
Fields: { hello: 'world', name: 'multipart' }
Files: [ { fieldname: 'file',
    originalname: 'test.txt',
    encoding: '7bit',
    mimetype: 'application/octet-stream',
    destination: 'temp',
    filename: '7218bd891a',
    path: 'temp/7218bd891a',
    size: 12 },
  { fieldname: 'picture',
    originalname: 'large.jpg',
    encoding: '7bit',
    mimetype: 'application/octet-stream',
    destination: 'temp',
    filename: 'e7a8050980',
    path: 'temp/e7a8050980',
    size: 1592548 } ]

FormDataFile

MultipartFormData adds a body object and a file or files object to the request object. The body hashmap contains the values of the text fields of the form, the file or files object contains the files uploaded via the form.

import('stream').Readable stream.Readable: A stream that pushes data when it becomes available.

FormDataFile: The information about each file.

NameTypeDescription
fieldname*stringThe field name specified in the form.
originalname*stringThe name of the file on the user's computer.
encoding*stringThe encoding type of the file.
mimetype*stringThe mime type of the file.
size*numberThe size of the file in bytes.
destination*stringThe folder to which the file has been saved. Set by DiskStorage.
filename*stringThe name of the file within the destination. Set by DiskStorage.
path*stringThe full path to the uploaded file. Set by DiskStorage.
buffer*BufferThe Buffer of the entire file. Set by MemoryStorage.
stream*stream.ReadableThe Readable stream with the file data. This stream should not be read other than by a storage engine.

GNU Affero General Public License v3.0

Original work by Multer's contributors under MIT license found in COPYING.

Art Deco © Art Deco for Idio 2019 Idio Tech Nation Visa Tech Nation Visa Sucks

Keywords

multer

FAQs

Package last updated on 28 Dec 2019

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