Package Exports
- catchment
This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (catchment) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.
Readme
catchment
A Node.js package to collect stream data into a catchment and return it either as a buffer or a string.
yarn add -E catchmentTable of Contents
API
The package exports the default Catchment class.
import Catchment 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-dataconstructor(
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.
| Name | Type | Description | Default |
|---|---|---|---|
| rs | Readable | A 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. | - |
| binary | boolean | Whether 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'Copyright
(c) Art Deco 2018