# @marblejs/middleware-multipart

> A multipart/form-data middleware for Marble.js

Latest version **4.1.0** (published 2023-03-06) · MIT license · 0 weekly downloads

## Install

```sh
npm install @marblejs/middleware-multipart
pnpm add @marblejs/middleware-multipart
yarn add @marblejs/middleware-multipart
bun add @marblejs/middleware-multipart
```

## Health

**Score 25/100 (F)** — status: abandoned.

Positive: has types; no vulnerabilities; high quality score.

Warnings: low downloads; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 4.1.0 |
| Published | 2023-03-06 |
| First published | 2019-06-25 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | CommonJS |
| Node | >= 8.0.0 |
| Dependencies | 1 |
| Unpacked size | 20.2 KB |
| Known vulnerabilities | 0 (+2 in 1 direct dependencies) |
| Install scripts | no |
| GitHub stars | 2146 |
| Author | marblejs |
| Maintainers | jflakus |
| Keywords | marble.js, multipart, middleware, http, rxjs |

## Links

- npm: https://www.npmjs.com/package/@marblejs/middleware-multipart
- Repository: https://github.com/marblejs/marble
- Homepage: https://github.com/marblejs/marble#readme
- Issues: https://github.com/marblejs/marble/issues
- npm.io page: https://npm.io/package/@marblejs/middleware-multipart

## Dependencies (1)

- [@fastify/busboy](https://npm.io/package/@fastify/busboy.md) ^1.0.0

## Alternatives

- [@sindresorhus/slugify](https://npm.io/package/@sindresorhus/slugify.md) — 3.7M weekly downloads
- [solid-js](https://npm.io/package/solid-js.md) — 2.7M weekly downloads
- [expo-glass-effect](https://npm.io/package/expo-glass-effect.md) — 2.5M weekly downloads
- [nanoassert](https://npm.io/package/nanoassert.md) — 780.8K weekly downloads
- [@ffmpeg/ffmpeg](https://npm.io/package/@ffmpeg/ffmpeg.md) — 529.5K weekly downloads

## Recent versions

- 4.1.0 (latest) — 2023-03-06
- 4.0.0-rc.3 (rc) — 2021-09-21
- 4.0.0-next.32 (next) — 2021-08-26
- 3.4.2-dev.6 (dev) — 2020-09-17
- 4.0.3 — 2022-01-27
- 4.0.2 — 2021-12-08
- 4.0.1 — 2021-10-19
- 3.5.2 — 2021-10-19
- 4.0.0 — 2021-10-11
- 4.0.0-rc.2 — 2021-09-02
- 4.0.0-rc.1 — 2021-08-26
- 4.0.0-rc.0 — 2021-08-26
- 4.0.0-next.28 — 2021-08-25
- 4.0.0-next.27 — 2021-08-24
- 4.0.0-next.26 — 2021-08-24
- … 75 more at https://npm.io/package/@marblejs/middleware-multipart/versions

## README

<p align="center">
  <a href="https://marblejs.com">
    <img src="https://github.com/marblejs/marble/blob/master/assets/img/logo.png?raw=true" width="200" alt="Marble.js logo"/>
  </a>
</p>

# @marblejs/middleware-multipart

A **multipart/form-data** middleware for [Marble.js](https://github.com/marblejs/marble) based on [busboy](https://github.com/mscdex/busboy) library.

## Installation

```
$ npm i @marblejs/middleware-multipart
```
Requires `@marblejs/core` and `@marblejs/http` to be installed.

## Documentation

For the latest updates, documentation, change log, and release information visit [docs.marblejs.com](https://docs.marblejs.com) and follow [@marble_js](https://twitter.com/marble_js) on Twitter.

## Usage

---

**WARNING**: Make sure that you always handle the files that a user uploads. Never add it as a global middleware since a malicious user could upload files to a route that you didn't anticipate. Only use this it on routes where you are handling the uploaded files.

---

By default middleware parses all incoming files and stores them in-memory.

*Keep in mind that the middleware will parse only `POST` and `PATCH` request with multipart content-type.*

```typescript
import { multipart$ } from '@marblejs/middleware-multipart';

const effect$ = r.pipe(
  r.matchPath('/'),
  r.matchType('POST'),
  r.useEffect(req$ => req$.pipe(
    multipart$(),
    map(req => ({ body: {
      files: req.files['image_1'],  // file information
      body: req.body,             // all incoming body fields
    }}))
  )));
```

You can intercept incoming files and stream them to the different place, eg. to OS filesystem:

```typescript
import { multipart$, StreamHandler } from '@marblejs/middleware-multipart';

const stream: StreamHandler = ({ file, fieldname }) => {
  const destination = // ...
  return of({ destination });
};

const effect$ = r.pipe(
  r.matchPath('/'),
  r.matchType('POST'),
  r.useEffect(req$ => req$.pipe(
    multipart$({
      stream,
      maxFileCount: 1,
      files: ['image_1'],
    }),
    map(req => ({ body: {
      files: req.files['image_1'],  // file information
      body: req.body,             // all incoming body fields
    }}))
  )));
```

Each file contains the following information:

| key           | description |
| ------------- | ----------- |
| `fieldname`   | Field name specified in the form |
| `filename`    | Name of the file on the user's computer |
| `encoding`    | Encoding type of the file |
| `mimetype`    | Mime type of the file |
| `size`        | Size of the file in bytes |
| `destination` | The place in which the file has been saved (*if not in-memory*) |
| `buffer`      | A `Buffer` of the entire file |

You can define the following middleware options:

| key             | description |
| --------------- | ----------- |
| `maxFileCount`  | The total count of files that can be sent |
| `maxFieldCount` | The total count of fields that can be sent |
| `maxFileSize`   | The max possible file size in bytes |
| `files`         | An array of acceptable field names |
| `stream`        | A handler which you can use to stream incoming files to different location |

License: MIT

---
_Source: https://npm.io/package/@marblejs/middleware-multipart · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
