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

@heviir/async-request

v1.0.0

Published

Helper for making requests with native NodeJS http / https module

Downloads

4

Readme

Async Request

Utility for making requests in NodeJS using the internal HTTP / HTTPS library.

Basic request

// returns AsyncClientRequest & Promise<AsyncIncomingMessage>
const req = request('https://sample-url.com');

// calling .end() will end the request and return Promise<AsyncIncomingMessage>
// without calling .end(), the Promise<AsyncIncomingMessage> will never resolve
const res = await req.end();

// Only one of these can be used at a time to consume the response body
const buffer = await res.buffer(); // reads response body to Buffer
const text = await res.text(); // res.buffer() + Buffer.toString('utf-8')
const json = await res.json(); // res.buffer() + Buffer.toString('utf-8') + JSON.parse

Shorthands

await request.get('https://sample-url.com').end();
await request.post('https://sample-url.com').end();
await request.put('https://sample-url.com').end();
await request.delete('https://sample-url.com').end();
await request.options('https://sample-url.com').end();
await request.head('https://sample-url.com').end();
await request.connect('https://sample-url.com').end();

Sending data

const body = JSON.stringify({});
const res = await request
  .post('https://sample-url.com', {
    headers: {
      'content-type': 'application/json',
      'content-length': Buffer.byteLength(body),
    },
  })
  .end(body);

Streaming data from file to request

// NOTICE: .end() is not called, as piping to readStream calls it automatically
const req = request('https://sample-url.com');
fs.createReadStream('path/to/file.ext').pipe(req);
const res = await req;

With async pipeline

⚠️ WARNING! Does not work on windows

const asyncPipeline = promisify(pipeline);
const req = request('https://sample-url.com');
await asyncPipeline(fs.createReadStream('path/to/file.ext'), req);
const res = await req;

Streaming data from response to file

const asyncPipeline = promisify(pipeline);
const res = await request('https://sample-url.com');
await asyncPipeline(res, fs.createWriteStream('path/to/file.ext'));

Could also be done with just res.pipe, just not as elegant

const res = await request('https://sample-url.com').end();
res.pipe(fs.createWriteStream('path/to/file.ext'));
// Make sure that writing the response body to file is finished
// after following line
await new Promise(resolve => res.once('end', resolve));

Options

const res = await request(
  // if protocol is https user NodeJS https.request else http.request
  'https://sample-url.com',
  // optional options object
  {
    // basePath, eg path: '/hello/there' => https://sample-url.com/hello/there
    path: '',
    // headers object, will be added to request
    headers: {},
    // override default agent
    agent: new https.Agent(),
    // query object, will be stringified and merged to url
    query: {},
    // request method 'GET' | 'POST' | 'PUT' etc...
    // default 'GET'
    method: 'GET',
  },
);