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-pipe

v0.4.0

Published

multipart-pipe ==============

Downloads

14

Readme

multipart-pipe

Pipe multipart uploads direct to S3 or another file service in connect middleware without writing to disk. It uses multiparty to parse the multipart form.

It is tested and soon to be used in production.

Note: If you are coming from the 0.2.2 or below version, the API has changed because express.multipart is deprecated and no nice multipart middleware exists to replace it, this package had to replicate some of that functionality. Now the only middleware you apply is the one in this package, and two new options (encoding and byte limits) are added to support functionality lost from express to multiparty.

Install

npm install multipart-pipe

Usage

var multipartPipe = require('multipart-pipe'),
  knox = require('knox'),
  express = require('express')

var app = express(), /* or connect() */
  s3 = knox.createClient({
    bucket: my_bucket,
    key: my_key,
    secret: my_secret
  })

// Pipes to S3
app.use(multipartPipe.s3(s3))

Results

In the request object after the pipe middleware will be two new fields on the request object:

  • a new req.files field which will contain a map of filenames prior to upload to filenames on the streamed-to fileserver.
  • a req.form field with a map of form field values that might have also come with the multipart file.

For example:

app.use('/upload', multipartPipe.s3(s3))
app.post('/upload', function (req, res) {
  res.send({
    ok: true,
    uploaded_files: req.files,
    other_fields: req.form
  })
})

Options

The main way to instantiate the middleware is multipartPipe(options) where options contains the following:

  • streamer - Required function (part, filename, callback)
    • Optionally call multipartPipe.s3(s3_knox_client, options) to use built-in S3 streamer
  • allow - Optional String or RegExp to test each part's content-type header for acceptability
  • filename - Optional function (part_filename, part_content_type) which returns a filename to store. Defaults to function (part_filename) { return part_filename; }
  • encoding - Set the encoding. Defaults to the usual utf8.
  • limit - Set a bytesReceived limit. Can be in string form like '128mb', '1gb', '512kb'. Defaults to `128mb'.

S3 Options

When using pipe.s3(s3_knox_client, opts), there are additional options:

  • headers - Optional object with default headers for each upload to S3. Defaults to enabling public-read.

Useful Things To Know

  • Limit upload size:

    app.use(multipartPipe.s3(s3, { limit: '128mb' }))
  • Limit content types (to say, just images):

    app.use(multipartPipe.s3(s3, {
      allow: /^image\/.*$/
    }))
  • Use uploaded filename with counter and a path parameter prepended:

    var counter = 0;
    app.use(multipartPipe.s3(s3, {
      filename: function (fn, mime) {
        return req.params.prefix + '/' + (counter++) + '_' + fn
      }
    }))
  • Find the right mime type extension for a filename. Referenced package: mime

    var mime = require('mime'),
      path = require('path');
    
    app.use(multipartPipe.s3(s3, {
      filename: function (fn, contentType) {
        return "some-directory/" + path.basename(fn).split('.')[0] + '.' + mime.extension(contentType);
      }
    }))
  • Create your own streamer function

    function streamer(part, filename, callback) {
      // see source s3streamer() for example
    }
    
    app.use(multipartPipe({streamer: streamer}))
  • Restrict the middleware to a specific path

    app.use('/upload', multipartPipe.s3(s3, opts))

License

MIT in LICENSE file.