Launch Week Day 5: Introducing Reachability for PHP.Learn More
Socket
Book a DemoSign in
Socket

milliparsec

Package Overview
Dependencies
Maintainers
1
Versions
29
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

milliparsec

tiniest body parser in the universe

Source
npmnpm
Version
4.0.0
Version published
Maintainers
1
Created
Source





Vulnerabilities Version Coverage Github actions Downloads


Tiniest body parser in the universe. Built for modern Node.js.

Check out deno-libs/parsec for Deno port.

Features

  • ⏩ built with async / await
  • 🛠 JSON / raw / urlencoded data support
  • 📦 tiny package size (8KB dist size)
  • 🔥 no dependencies
  • tinyhttp and Express support
  • ⚡ 30% faster than body-parser

Install

# pnpm
pnpm i milliparsec

# bun
bun i milliparsec

Usage

Basic example

Use a middleware inside a server:

import { createServer } from 'node:http'
import { json } from 'milliparsec'

const server = createServer(async (req: ReqWithBody, res) => {
  await json()(req, res, (err) => void err && console.log(err))

  res.setHeader('Content-Type', 'application/json')

  res.end(JSON.stringify(req.body))
})

Web frameworks integration

tinyhttp

import { App } from '@tinyhttp/app'
import { urlencoded } from 'milliparsec'

new App()
  .use(urlencoded())
  .post('/', (req, res) => void res.send(req.body))
  .listen(3000, () => console.log(`Started on http://localhost:3000`))

API

raw(req, res, cb)

Minimal body parsing without any formatting.

text(req, res, cb)

Converts request body to string.

urlencoded(req, res, cb)

Parses request body using new URLSearchParams.

json(req, res, cb)

Parses request body using JSON.parse.

multipart(req, res, cb)

Parses request body using multipart/form-data content type and boundary. Supports files as well.

// curl -F "textfield=textfield" -F "someother=textfield with text" localhost:3000
await multipart()(req, res, (err) => void err && console.log(err))
res.end(req.body) // { textfield: "textfield", someother: "textfield with text" }

custom(fn)(req, res, cb)

Custom function for parsec.

// curl -d "this text must be uppercased" localhost:3000
await custom(
  req,
  (d) => d.toUpperCase(),
  (err) => {}
)
res.end(req.body) // "THIS TEXT MUST BE UPPERCASED"

What is "parsec"?

The parsec is a unit of length used to measure large distances to astronomical objects outside the Solar System.

Keywords

body-parser

FAQs

Package last updated on 11 Sep 2024

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts