UNPKG

6.25 kBMarkdownView Raw
1# catchment
2
3[![npm version](https://badge.fury.io/js/catchment.svg)](https://npmjs.org/package/catchment)
4
5A Node.js package to collect stream data into a catchment and return it either as a buffer or a string.
6
7```
8yarn add -E catchment
9```
10
11## Table of Contents
12
13- [Table of Contents](#table-of-contents)
14- [API](#api)
15- [`Catchment` Class](#catchment-class)
16 * [`constructor(options?: Options): Catchment`](#constructoroptions-options-catchment)
17 * [`Options`](#options)
18 * [Collect Buffer](#collect-buffer)
19 * [Pipe Readable](#pipe-readable)
20- [`async collect(readable: Readable, options?: CollectOptions): string|Buffer`](#async-collectreadable-readableoptions-collectoptions-stringbuffer)
21 * [`CollectOptions`](#collectoptions)
22- [Errors Handling](#errors-handling)
23- [Copyright](#copyright)
24
25## API
26
27The package exports the default _Catchment_ class, and the `collect` method.
28
29```js
30import Catchment, { collect } from 'catchment'
31```
32
33## `Catchment` Class
34
35_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.
36
37A new _Catchment_ can be created with a constructor, which accepts [optional options](#options).
38
39```javascript
40/* yarn example/catchment.js */
41import { Readable } from 'stream'
42import Catchment from 'catchment'
43
44const DATA = 'test-data'
45
46// creating a readable stream to use in the example
47const rs = new Readable({
48 read() {
49 for (let i = 0; i < DATA.length; i++) {
50 const c = DATA.charAt(i)
51 this.push(c)
52 }
53 this.push(null)
54 },
55})
56
57;(async () => {
58 try {
59 const catchment = new Catchment()
60 rs.pipe(catchment)
61 const res = await catchment.promise
62 console.log(res)
63 } catch (err) {
64 console.log(err)
65 }
66})()
67```
68
69```
70test-data
71```
72
73### `constructor(`<br/>&nbsp;&nbsp;`options?: Options,`<br/>`): Catchment`
74
75### `Options`
76
77An optional options object can be passed to the constructor.
78
79`import('stream').Readable` __<a name="readable">`Readable`</a>__
80
81__`Options`__: Options to pass to the `Writable` super constructor, and others shown below.
82
83| Name | Type | Description | Default |
84| ---- | ---- | ----------- | ------- |
85| rs | [_Readable_](#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. | - |
86| 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` |
87
88#### Collect Buffer
89
90To receive a buffer, the `binary` option should be set to `true`:
91
92```javascript
93/* yarn example/binary.js */
94import Catchment from 'catchment'
95import { createReadable } from './lib'
96
97(async () => {
98 try {
99 const rs = createReadable('test-data')
100 const catchment = new Catchment({ binary: true })
101 rs.pipe(catchment)
102
103 const res = await catchment.promise
104 console.log(res)
105 } catch (err) {
106 console.log(err)
107 }
108})()
109```
110
111```
112<Buffer 74 65 73 74 2d 64 61 74 61>
113```
114
115#### Pipe Readable
116
117To automatically pipe a _Readable_, and reject the promise if an error occurs there, the `rs` option can be passed:
118
119```js
120/* yarn example/rs.js */
121import Catchment from 'catchment'
122import { createReadStream } from 'fs'
123
124(async () => {
125 try {
126 const rs = createReadStream('missing-file.txt')
127 const { promise } = new Catchment({ rs })
128
129 const res = await promise
130 console.log(res)
131 } catch ({ message }) {
132 console.log(message)
133 }
134})()
135```
136
137```
138ENOENT: no such file or directory, open 'missing-file.txt'
139```
140
141
142
143## `async collect(`<br/>&nbsp;&nbsp;`readable: Readable,`<br/>&nbsp;&nbsp;`options?: CollectOptions,`<br/>`): string|Buffer`
144
145The 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.
146
147Some options can be passed to the `collect` method.
148
149__<a name="collectoptions">`CollectOptions`</a>__: Options when collecting data into a catchment. They can extend `Writable` options which will be passed to the `Catchment` constructor.
150
151| Name | Type | Description | Default |
152| ---- | ---- | ----------- | ------- |
153| 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` |
154
155## Errors Handling
156
157Whenever 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.
158
159If the error has a stack, it will be modified to clean it from internal Node.js lines, such as `_module`.
160
161```js
162import { Readable } from 'stream'
163import Catchment from 'catchment'
164
165const rs = new Readable({
166 read() {
167 const er = new Error('example-error')
168 this.emit('error', er) // emit an error to reject catchment
169 this.push(null)
170 },
171})
172
173;(async () => {
174 try {
175 const catchment = new Catchment({
176 rs,
177 })
178 rs.pipe(catchment)
179 await catchment.promise
180 } catch ({ stack }) {
181 console.log(stack)
182 }
183})()
184```
185
186```
187Error: example-error
188 at Readable.read [as _read] (/Users/zavr/adc/catchment/example/error-catchment.js:6:16)
189```
190
191If 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.
192
193```js
194import { createReadStream } from 'fs'
195import { collect } from 'catchment'
196
197(async () => {
198 try {
199 const rs = createReadStream('missing-file.txt')
200 await collect(rs)
201 } catch ({ stack }) {
202 console.log(stack)
203 }
204})()
205```
206
207```
208Error: ENOENT: no such file or directory, open 'missing-file.txt'
209 at /Users/zavr/adc/catchment/example/error-collect.js:7:11
210 at Object.<anonymous> (/Users/zavr/adc/catchment/example/error-collect.js:11:3)
211```
212
213## Copyright
214
215(c) [Art Deco][1] 2018
216
217[1]: https://artdeco.bz