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

@kakang/fastify-multipart

v1.1.2

Published

[![Continuous Integration](https://github.com/kaka-repo/fastify-plugins/actions/workflows/ci-multipart.yml/badge.svg)](https://github.com/kaka-repo/fastify-plugins/actions/workflows/ci-multipart.yml) [![NPM version](https://img.shields.io/npm/v/@kakang/fa

Downloads

253

Readme

@kakang/fastify-multipart

Continuous Integration NPM version

This plugin is used to replace fastify-formidable and fastify-busboy. It provides a generic API which allows users to not only using formidable or busboy for parsing multipart/form-data, but provide custom adapter for parsing and custom storage engine to handle file stream. You may also try the async iterator same as @fastify/multipart for advanced usage.

You can checkout @fastify/multipart if you prefer offcial plugin.

Install

npm install @kakang/fastify-multipart --save

yarn add @kakang/fastify-multipart

// For BusboyAdapter
npm install busboy --save

yarn add busboy

// For FormidableAdapter
npm install formidable --save

yarn add formidable

Usage

import FastifyMultipart from '@kakang/fastify-multipart'
import { BusboyAdapter } from '@kakang/fastify-multipart/lib/adapter/busboy'
import { FileStorage } from '@kakang/fastify-multipart/lib/storage/file'
import { BufferStorage } from '@kakang/fastify-multipart/lib/storage/buffer'

fastify.register(FastifyMultipart, {
  adapter: BusboyAdapter,
  storage: FileStorage,
  storageOption: {
    uploadDir: '/'
  }
})

fastify.post('/', async function(request, reply) {
  // you need to call the parser if you do not pass any option through plugin registration
  await request.parseMultipart()

  // you can use async iterator for file handling
  for await (const { type, name, value, info } of request.multipart()) {
    switch(type) {
      case 'field': {
        console.log(name, value)
        break
      }
      case 'file': {
        console.log(name, value, info.filename)
        // value is Readable
        value.resume()
        break
      }
    }
  }

  // access files
  request.files

  // access body
  // note that file fields will exist in body and it will becomes the file path saved on disk
  request.body

  // check if it is multipart
  if(request[kIsMultipart] === true) {}

  // check if it is already parsed
  if (request[kIsMultipartParsed] === true) {}
})


// add content type parser which will automatic parse all `multipart/form-data` found
fastify.register(FastifyBusboy, {
  addContentTypeParser: true,
  adapter: BusboyAdapter,
  storage: BufferStorage
})

// add `preValidation` hook which will automatic parse all `multipart/form-data` found
fastify.register(FastifyBusboy, {
  addHooks: true,
  adapter: BusboyAdapter,
  storage: BufferStorage
})

Options

options.adapter

The option for content type parser adapter. By default, we provide BusboyAdapter and FormidableAdapter. You may also extends Adapter and built your own one.

import FastifyMultipart from '@kakang/fastify-multipart'
import { Adapter } from '@kakang/fastify-multipart/lib/adapter/adapter'
import { BusboyAdapter } from '@kakang/fastify-multipart/lib/adapter/busboy'
import { FormidableAdapter } from '@kakang/fastify-multipart/lib/adapter/formidable'
import { BufferStorage } from '@kakang/fastify-multipart/lib/storage/buffer'

fastify.register(FastifyBusboy, {
  adapter: BusboyAdapter,
  storage: BufferStorage,
})

options.storage

The option for storage engine. By default, we provide FileStorage and BufferStorage. You may also extends Storage and built your own one.

import FastifyMultipart from '@kakang/fastify-multipart'
import { BusboyAdapter } from '@kakang/fastify-multipart/lib/adapter/busboy'
import { Storage } from '@kakang/fastify-multipart/lib/storage/storage'
import { BufferStorage } from '@kakang/fastify-multipart/lib/storage/buffer'
import { FileStorage } from '@kakang/fastify-multipart/lib/storage/file'

fastify.register(FastifyBusboy, {
  adapter: BusboyAdapter,
  storage: MemoryStorage,
})

options.removeFilesFromBody

This options will not add any files fields to body when enabled.

import FastifyMultipart from '@kakang/fastify-multipart'
import { BusboyAdapter } from '@kakang/fastify-multipart/lib/adapter/busboy'
import { BufferStorage } from '@kakang/fastify-multipart/lib/storage/buffer'

fastify.register(FastifyBusboy, {
  adapter: BusboyAdapter,
  storage: BufferStorage,
  removeFilesFromBody: true
})