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

uws-connect

v1.2.3

Published

Use connect like middlewares with uWebSockets.js

Downloads

80

Readme

uws-connect

Use connect like middlewares with uWebSockets.js.

Provides support for

  • connect helper for connecting connect, express middlewares
  • body-parser (json, form-urlencoded)
  • nodejs streams support for uWS.HttpRequest and uWS.HttpResponse (may not be 100% compliant with nodejs streams)
  • final handler for errors

The design aims to be as fast and unopinionated as possible.

All parts provided can also be used as single building blocks.

Table of Contents

Installation

npm install uws-connect

Usage

import { App, bodyParser, params } from 'uws-connect'
import { Transform } from 'stream'

const app = App()

app.use((req, res, next) => { // a simple logging mw applied to all routes
  const { method, url } = req
  console.log('%s %s', method, url)
  next()
})
app.get('/',
  // a connect like middleware
  (req, res, next) => {
    next()
    },
  // async middleware (no need for `next()` or `try {} catch (err) {}`)
  async (req, res) => {
    res.body = await something()
  },
  (req, res) => {
    res.end(res.body)
  }
)
app.put('/users/:user',
  // get `req.params` compatibility
  // NOTE: use the same route as the router here!
  params('/users/:user'),
  // does json or form body parsing.
  bodyParser({ limit: 100000 }),
  (req, res) => {
    // restana like res.send method
    // res.send(data: any, status?: number, headers?: object) => void
    res.send({
      params: req.params, // from `params()` middleware
      body: req.body,     // from `bodyParser()` middleware
    })
  }
)

// if stream support is needed...
const transform = new Transform({
  transform (chunk, enc, cb) {
    this.push(_chunk)
    cb()
  }
})
app.post('/echo',
  (req, res) => { req.pipe(transform).pipe(res) }
)

app.listen(9001)

If you need to better fine tune the performance of your app and don't want to trade speed, use glue(...handlers) for connecting middlewares.

// same as `import uWs from 'uWebSockets.js'`
import { uWS, connect } from 'uws-connect'
import cors from 'cors'
const uwsApp = uWS.App()

// just uWS, as fast as fast can be
// NOTE: (response, request) for uWS handler
uwsApp.get('/', (response, request) => response.end('done'))

// use some routes with connect middlewares (like cors)
// NOTE: glue() uses express (req, res, next) handlers!
const _cors = cors()
const glue = connect()
uwsApp.options('/*', glue(_cors))
uwsApp.get('/with-cors', glue(
  _cors,
  (req, res) => res.end('with cors'))
)

uwsApp.listen(9001, () => {})

Benchmarks

$ cd benchmark
$ node index.js -d 10 -c 2500 -p 4

*) Results may vary on your machine.

| Package | Version | Requests/s | Latency (ms) | Throughput (Mb) | | :---------- | ------: | ---------: | -----------: | --------------: | | uWebSockets | 20.42.0 | 199909 | 85.06 | 20.40 | | uws-connect | 1.2.3 | 176311 | 100.07 | 15.13 | | native | v21.6.2 | 66488 | 76.57 | 8.69 | | polka | 0.5.2 | 61027 | 80.09 | 7.97 | | restana | 4.9.7 | 60422 | 68.04 | 7.89 | | express | 4.18.3 | 16915 | 125.88 | 2.21 |

Contributing

Your help is appreciated. File an issue and fork this project to contribute with your ideas.

Please follow the minimalistic approach as chosen here. Keep things simple.

If you contribute code to this project, you are implicitly allowing your code to be distributed under the MIT license. You are also implicitly verifying that all code is your original work or correctly attributed with the source of its origin and license.

The Code-of-Conduct is Contributor Covenant Code of Conduct.

License

MIT Licensed

References