1.2.0 • Published 9 months ago

easy-busboy v1.2.0

Weekly downloads
-
License
MIT
Repository
github
Last release
9 months ago

Easy, promise-like typed Busboy wrapper (works with firebase cloud functions)

badge badge badge badge

Latest change:
  • fix support for firebase cloud functions,
  • reduced package size
Built with:

badge badge badge

Multipart/form-data uploads with one-liner

  • use one-liner instead of event listeners to consume Busboy functionality, no other dependencies,
  • based on Busboy,
  • to be used with Express (4 & 5) and Koa or as a part of firebase cloud function,
  • WIP option to specify the way file stream gets processed to Buffer ('memory' | 'storage'),
  • typed and covered with tests,

Standard usage (using await syntax)

import { easyBusboy } from 'easy-busboy';

// Express 4 or 5 (the same for firebase cloud function)
app.post<{ fields: IFields; files: IFiles }>(
  '/upload-file',
  async (req, res) => {
    const { fields, files } = await easyBusboy(req);
    res.send({ fields, files });
  }
);

// Koa
app.use(async (ctx) => {
  const { fields, files } = await easyBusboy(ctx.req);
  ctx.body = { fields, files };
});

Response format

No data is being lost while parsing, below is the response interface:

{
  files: Record<
    string,
    {
      buffer: Buffer;
      info: FileInfo; // imported from Busboy
    }
  >;
  fields: Record<
    string,
    {
      value: string;
      info: FieldInfo; // imported from Busboy
    }
  >;
}

Providing Busboy config

import { easyBusboy } from 'easy-busboy';

...
const { fields, files } = await easyBusboy(req, {
      processStreamsMethod: 'memory' | 'storage', // [WIP] default 'memory'
      limits: cfg.limits, // see busboy config limits
      headers,
      conType, // content type
      highWaterMark: ...,
      fileHwm: ...,
      defCharset: ...,
      defParamCharset: ...,
      preservePath: ...,
    });
...

How it works?

It is just a simple method which encapsules Busboy onFile/onField (and other) events in a promise then creates key/value pairs for found fields/files

Small note - if multiple fields with the same name are provided in request then response is going to contain all fields indexed accordingly (no duplicates boss, sorry)

WIP Specify files processing method

You can specify processStreamsMethod in config, it may be:

  • storage - so that file streams will be converted into Buffers using temporary directory,
  • memory - above will be achieved without saving temporary file

In first case file will be returned as a Buffer, in second it is a URI pointing temporary file path

Examples (see github repository)

Before implementing package in your project you can check out functionality using attached examples you can find there Express servers already utilizing easyBusboy method as well as example client (axios)

To be able to work with examples install dependencies in root folder first

  • pnpm i,

then take a look at folders mentioned above and package.json scripts:

  • pnpm run examples:servers:install (this one installs deps for servers examples),
  • pnpm run examples:servers:express4:start {PORT} (run Express 4 server) (you can replace express4 here with 'express5' or 'koa'), note PORT is optional and by default equals 3000,
  • pnpm run examples:servers:clean (this one removes deps for servers examples),

Finally when server is listening either launch some example client (look at package.json scripts) providing correct {PORT} as an argument (the same way as with server script) or launch Postman Postman and play with requests to localhost:{PORT}/upload-file !

Tests

  • pnpm test to run,
  • lib/*test.ts contains some positive/negative test scenarios clearly explaining functionality,

Coverage

File% Stmts% Branch% Funcs% LinesUncovered Lines
All files88.4707094.91
index.ts865064.2893.33122-123,128
utils.ts94.7310083.33100
1.2.0

9 months ago

1.1.6

9 months ago

1.1.5

9 months ago

1.1.4

9 months ago

1.1.3

9 months ago

1.1.2

9 months ago

1.1.1

9 months ago

1.1.0

9 months ago

1.0.11

9 months ago

1.0.10

9 months ago

1.0.9

9 months ago

1.0.8

9 months ago

1.0.7

9 months ago

1.0.6

9 months ago

1.0.5

9 months ago

1.0.4

9 months ago

1.0.3

9 months ago

1.0.2

9 months ago

1.0.1

9 months ago

1.0.0

9 months ago

0.0.0

9 months ago