@binden/multipart
TypeScript icon, indicating that this package has built-in type declarations

2.0.1 • Public • Published

@binden/multipart CI Status version Known Vulnerabilities Coverage Status code style: prettier Contributor Covenant semantic-release Conventional Commits GitHub top language node version npm downloads License

Binden multipart/form-data parser middleware (based on busboy).

Installation

npm install @binden/multipart

Usage

import multipart from "@binden/multipart";

const formDataParser = multipart({ limits: { fields: 2, fileSize: 1024 } });

const next = (context) => {
  if (context.request.body instanceof FormData) {
    context.log.info("Body has been parsed successfully");
    return context.status(200).text("GOOD");
  } else {
    context.log.warn("Body is not a valid `form-data`");
    return context.status(400).text("BAD");
  }
};

app.use("/formdata", new Router().post(formDataParser, next));

Truncated FormData

Set the throw_limits to false to avoid throwing errors. FormData might be truncated due to config.limits (See busboy limits)

import { Multipart } from "@binden/multipart";

const formDataParser = new Multipart({
  throw_limits: false,
  config: {
    limits: {
      fieldNameSize: 5,
      fieldSize: 16 * 1024,
      fields: 4,
      fileSize: 128 * 1024,
      files: 4,
      parts: 6,
    },
  },
});

const next = (context) => {
  if (context.request.body instanceof FormData) {
    context.log.info("Fields and files might have been truncated");
    return context.status(200).text("MAYBE GOOD");
  } else {
    context.log.warn("Body is not a valid `form-data`");
    return context.status(400).text("BAD");
  }
};

app.use("/formdata", new Router().post(formDataParser, next));

Test

npm test

Package Sidebar

Install

npm i @binden/multipart

Weekly Downloads

1

Version

2.0.1

License

AGPL-3.0-only

Unpacked Size

44.5 kB

Total Files

6

Last publish

Collaborators

  • vansergen