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

capacitance

Package Overview
Dependencies
Maintainers
1
Versions
3
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

capacitance

Collapse stream to a promise.

  • 1.0.2
  • latest
  • npm
  • Socket score

Version published
Weekly downloads
2
Maintainers
1
Weekly downloads
 
Created
Source

Capacitance

Collapse stream to a promise.

Usage:

We are often write ugly code such as below:

let buffers = [];
stream.on('data', buffer => buffers.push(buffer));
stream.on('end', () => {
  console.log(Buffer.concat(buffers));
});

Use the Capacitance, just write:

stream.pipe(new Capacitance).then(data => {
  console.log(data);
});

Demo: read synchronously form data (ES7)

#!/usr/bin/env babel-node --stage 0

import Capacitance from 'capacitance';
import http from 'http';

http.createServer(async (req, res) => {
  switch(req.method) {
    case 'GET':
      res.writeHead(200, { 'Content-Type': 'text/html' });
      res.write(`
        <form method="post">
          <input name="test" value="xxx" />
          <input type="submit" value="Submit" />
        </form>
      `);
      return res.end();
    case 'POST':
      // Convert stream to a promise and await it as a synchronous data
      let data = await req.pipe(new Capacitance());
      res.writeHead(200, { 'Content-Type': 'text/plain' });
      res.write(data);
      return res.end();
  }
}).listen(8000);

FAQs

Package last updated on 10 Apr 2023

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