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

easy-busboy

v1.2.0

Published

Easy to use promise-based Busboy wrapper for Express & Koa

Downloads

1,237

Readme

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 | % Lines | Uncovered Lines | | --------- | ------- | -------- | ------- | ------- | --------------- | | All files | 88.4 | 70 | 70 | 94.91 | | index.ts | 86 | 50 | 64.28 | 93.33 | 122-123,128 | | utils.ts | 94.73 | 100 | 83.33 | 100 |