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

catchment

Package Overview
Dependencies
Maintainers
1
Versions
12
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

catchment

Collect stream data into a catchment.

  • 3.1.1
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
13K
decreased by-21.48%
Maintainers
1
Weekly downloads
 
Created
Source

catchment

npm version

A Node.js package to collect stream data into a catchment and return it either as a buffer or a string.

yarn add -E catchment

Table of Contents

API

The package exports the default Catchment class, and the collect method.

import Catchment, { collect } from 'catchment'

Catchment Class

Catchment extends Writable, and pushes incoming data into an internal array. When the stream finishes, a promise referenced in the promise property is fulfilled with concatenated data. If an error occurred, the promise is rejected.

A new Catchment can be created with a constructor, which accepts optional options.

/* yarn example/catchment.js */
import { Readable } from 'stream'
import Catchment from 'catchment'

const DATA = 'test-data'

// creating a readable stream to use in the example
const rs = new Readable({
  read() {
    for (let i = 0; i < DATA.length; i++) {
      const c = DATA.charAt(i)
      this.push(c)
    }
    this.push(null)
  },
})

;(async () => {
  try {
    const catchment = new Catchment()
    rs.pipe(catchment)
    const res = await catchment.promise
    console.log(res)
  } catch (err) {
    console.log(err)
  }
})()
test-data

constructor(
  options?: Options,
): Catchment

Options

An optional options object can be passed to the constructor.

import('stream').Readable Readable

Options: Options to pass to the Writable super constructor, and others shown below.

NameTypeDescriptionDefault
rsReadableA readable stream to automatically pipe into the catchment. If an error occurs during reading of this stream, the catchment promise will be rejected with it.-
binarybooleanWhether to return a raw buffer instead of a string. The string is created by joining all incoming chunks together with .join('') method.false
Collect Buffer

To receive a buffer, the binary option should be set to true:

/* yarn example/binary.js */
import Catchment from 'catchment'
import { createReadable } from './lib'

(async () => {
  try {
    const rs = createReadable('test-data')
    const catchment = new Catchment({ binary: true })
    rs.pipe(catchment)

    const res = await catchment.promise
    console.log(res)
  } catch (err) {
    console.log(err)
  }
})()
<Buffer 74 65 73 74 2d 64 61 74 61>
Pipe Readable

To automatically pipe a Readable, and reject the promise if an error occurs there, the rs option can be passed:

/* yarn example/rs.js */
import Catchment from 'catchment'
import { createReadStream } from 'fs'

(async () => {
  try {
    const rs = createReadStream('missing-file.txt')
    const { promise } = new Catchment({ rs })

    const res = await promise
    console.log(res)
  } catch ({ message }) {
    console.log(message)
  }
})()
ENOENT: no such file or directory, open 'missing-file.txt'

async collect(
  readable: Readable,
  options?: CollectOptions,
): string|Buffer

The collect method is a shorthand for creating a new catchment, and piping a readable stream into it. It will accumulate all data from the read stream, and asynchronously return when the stream finishes. If an error occurs in the stream, the promise will be rejected.

Some options can be passed to the collect method.

CollectOptions: Options when collecting data into a catchment. They can extend Writable options which will be passed to the Catchment constructor.

NameTypeDescriptionDefault
binarybooleanWhether to return a raw buffer instead of a string. The string is created by joining all incoming chunks together with .join('') method.false

Errors Handling

Whenever an error is encountered during reading a readable stream, either piped into a Catchment via the rs option, or passed as an argument to the collect method, it will result in a rejected promise.

If the error has a stack, it will be modified to clean it from internal Node.js lines, such as _module.

import { Readable } from 'stream'
import Catchment from 'catchment'

const rs = new Readable({
  read() {
    const er = new Error('example-error')
    this.emit('error', er) // emit an error to reject catchment
    this.push(null)
  },
})

;(async () => {
  try {
    const catchment = new Catchment({
      rs,
    })
    rs.pipe(catchment)
    await catchment.promise
  } catch ({ stack }) {
    console.log(stack)
  }
})()
Error: example-error
    at Readable.read [as _read] (/Users/zavr/adc/catchment/example/error-catchment.js:6:16)

If the error does not have the stack (which can happen when using createReadStream from the fs module), it will appear as thrown at the point of either creating an instance of Catchment, or calling the collect method.

import { createReadStream } from 'fs'
import { collect } from 'catchment'

(async () => {
  try {
    const rs = createReadStream('missing-file.txt')
    await collect(rs)
  } catch ({ stack }) {
    console.log(stack)
  }
})()
Error: ENOENT: no such file or directory, open 'missing-file.txt'
    at /Users/zavr/adc/catchment/example/error-collect.js:7:11
    at Object.<anonymous> (/Users/zavr/adc/catchment/example/error-collect.js:11:3)

(c) Art Deco 2018

Keywords

FAQs

Package last updated on 13 Sep 2018

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