Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

undici

Package Overview
Dependencies
Maintainers
3
Versions
220
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

undici

An HTTP/1.1 client, written from scratch for Node.js

  • 4.10.3
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
11M
decreased by-14.48%
Maintainers
3
Weekly downloads
 
Created

What is undici?

The undici npm package is a HTTP/1.1 client, written from scratch for Node.js, that is designed to be faster and more efficient than the built-in 'http' and 'https' modules. It provides a low-level API for making HTTP requests and can be used to build higher-level abstractions.

What are undici's main functionalities?

HTTP Request

Make an HTTP request and process the response. This is the basic functionality of undici, allowing you to send HTTP requests and receive responses.

const { request } = require('undici');

(async () => {
  const { statusCode, headers, body } = await request('https://example.com')
  console.log('response received', statusCode);
  for await (const data of body) {
    console.log('data', data);
  }
})();

HTTP Pool

Use a pool of connections to make HTTP requests. This is useful for making a large number of requests to the same server, as it reuses connections between requests.

const { Pool } = require('undici');

const pool = new Pool('https://example.com')

async function query() {
  const { body } = await pool.request({
    path: '/path',
    method: 'GET'
  })

  for await (const data of body) {
    console.log('data', data);
  }
}

query();

HTTP Stream

Stream an HTTP response to a file or another stream. This is useful for handling large responses that you don't want to hold in memory.

const { pipeline } = require('undici');
const fs = require('fs');

pipeline(
  'https://example.com',
  fs.createWriteStream('output.txt'),
  (err) => {
    if (err) {
      console.error('Pipeline failed', err);
    } else {
      console.log('Pipeline succeeded');
    }
  }
);

HTTP Upgrade

Upgrade an HTTP connection to another protocol, such as WebSockets. This is useful for protocols that start with an HTTP handshake and then upgrade to a different protocol.

const { connect } = require('undici');

(async () => {
  const { socket, statusCode, headers } = await connect({
    path: '/path',
    method: 'GET'
  });

  console.log('upgrade response', statusCode, headers);

  socket.on('data', (chunk) => {
    console.log('data', chunk.toString());
  });
})();

Other packages similar to undici

Keywords

FAQs

Package last updated on 24 Nov 2021

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

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc