npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@multipart/form-data

v1.0.1

Published

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

Downloads

352

Readme

@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.

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.

| Name | Type | Description | Default | | ------------ | ------------------------------------------------------------------------------------------------------------------------------------ | ----------------------------------------------------------------------------------------------------------------------------------------------------- | ------- | | dest | string | The directory where to store the files using the DiskStorage. If not specified, files will be saved in the system's temp directory (os.tmpdir()). | - | | storage | FormDataStorageEngine | An instance of a custom storage engine. | - | | fileFilter | FormDataFileFilter | The file filter. | - | | limits | _goa.BusBoyLimits | The limits of the uploaded data. | - | | preservePath | boolean | Whether to keep the full path of files instead of just the base name. | false |

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 }
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 } ]

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

| Name | Type | Description | | --------- | --------------- | ------------------------------- | | name* | string | The name of the field. | | maxCount | number | The 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 } ] }
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
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.

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

Copyright & License

GNU Affero General Public License v3.0

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