README.md (7405B)
1 <!-- 2 3 @license Apache-2.0 4 5 Copyright (c) 2018 The Stdlib Authors. 6 7 Licensed under the Apache License, Version 2.0 (the "License"); 8 you may not use this file except in compliance with the License. 9 You may obtain a copy of the License at 10 11 http://www.apache.org/licenses/LICENSE-2.0 12 13 Unless required by applicable law or agreed to in writing, software 14 distributed under the License is distributed on an "AS IS" BASIS, 15 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 16 See the License for the specific language governing permissions and 17 limitations under the License. 18 19 --> 20 21 # Constant Stream 22 23 > Create a [readable stream][readable-stream] which always streams the same value. 24 25 <section class="usage"> 26 27 ## Usage 28 29 ```javascript 30 var constantStream = require( '@stdlib/streams/node/from-constant' ); 31 ``` 32 33 <a name="constant-stream"></a> 34 35 #### constantStream( value\[, options] ) 36 37 Returns a [readable stream][readable-stream] which **always** streams the **same** `value`. 38 39 ```javascript 40 var inspectStream = require( '@stdlib/streams/node/inspect-sink' ); 41 42 var iStream; 43 var stream; 44 45 function log( chunk, i ) { 46 console.log( chunk.toString() ); 47 if ( i === 10 ) { 48 stream.destroy(); 49 } 50 } 51 52 stream = constantStream( 'beep' ); 53 iStream = inspectStream( log ); 54 55 stream.pipe( iStream ); 56 ``` 57 58 The function accepts the following `options`: 59 60 - **objectMode**: specifies whether a [stream][stream] should operate in [objectMode][object-mode]. Default: `false`. 61 - **encoding**: specifies how `Buffer` objects should be decoded to `strings`. Default: `null`. 62 - **highWaterMark**: specifies the maximum number of bytes to store in an internal buffer before pausing streaming. 63 - **sep**: separator used to join streamed data. This option is only applicable when a stream is **not** in [objectMode][object-mode]. Default: `'\n'`. 64 - **iter**: number of iterations. 65 66 To set [stream][stream] `options`, 67 68 ```javascript 69 var opts = { 70 'objectMode': true, 71 'encoding': 'utf8', 72 'highWaterMark': 64 73 }; 74 75 var stream = constantStream( 'beep', opts ); 76 ``` 77 78 By default, the function returns a [stream][stream] which streams an infinite number of values (i.e., the [stream][stream] will **never** end). To limit the number of streamed values, set the `iter` option. 79 80 ```javascript 81 var inspectStream = require( '@stdlib/streams/node/inspect-sink' ); 82 83 function log( chunk ) { 84 console.log( chunk.toString() ); 85 } 86 87 var opts = { 88 'iter': 10 89 }; 90 91 var stream = constantStream( 'beep', opts ); 92 var iStream = inspectStream( log ); 93 94 stream.pipe( iStream ); 95 ``` 96 97 By default, when not operating in [objectMode][object-mode], a returned [stream][stream] delineates streamed values using a newline character. To specify an alternative separator, set the `sep` option. 98 99 ```javascript 100 var inspectStream = require( '@stdlib/streams/node/inspect-sink' ); 101 102 function log( chunk ) { 103 console.log( chunk.toString() ); 104 } 105 106 var opts = { 107 'iter': 10, 108 'sep': ',' 109 }; 110 111 var stream = constantStream( 'beep', opts ); 112 var iStream = inspectStream( log ); 113 114 stream.pipe( iStream ); 115 ``` 116 117 * * * 118 119 #### constantStream.factory( \[value, ]\[options] ) 120 121 Returns a `function` for creating [readable streams][readable-stream] which **always** stream the **same** provided `value`. 122 123 ```javascript 124 var opts = { 125 'objectMode': true, 126 'encoding': 'utf8', 127 'highWaterMark': 64 128 }; 129 130 var createStream = constantStream.factory( opts ); 131 ``` 132 133 If provided a `value` to stream, the returned function returns [readable streams][readable-stream] which **always** stream the **same** `value`. 134 135 ```javascript 136 var createStream = constantStream.factory( 'beep' ); 137 138 var stream1 = createStream(); 139 var stream2 = createStream(); 140 // ... 141 ``` 142 143 If not provided a `value` to stream, the returned function requires that a `value` be provided at each invocation. 144 145 ```javascript 146 var createStream = constantStream.factory(); 147 148 var stream1 = createStream( 'beep' ); 149 var stream2 = createStream( 'boop' ); 150 // ... 151 ``` 152 153 The method accepts the same `options` as [`constantStream()`](#constant-stream). 154 155 * * * 156 157 #### constantStream.objectMode( value\[, options] ) 158 159 This method is a convenience function to create [streams][stream] which **always** operate in [objectMode][object-mode]. 160 161 ```javascript 162 var inspectStream = require( '@stdlib/streams/node/inspect-sink' ); 163 164 function log( v ) { 165 console.log( v ); 166 } 167 168 var value = { 169 'beep': 'boop' 170 }; 171 var opts = { 172 'iter': 10 173 }; 174 var stream = constantStream.objectMode( value, opts ); 175 176 opts = { 177 'objectMode': true 178 }; 179 var iStream = inspectStream( opts, log ); 180 181 stream.pipe( iStream ); 182 ``` 183 184 This method accepts the same `options` as [`constantStream()`](#constant-stream); however, the method will **always** override the [`objectMode`][object-mode] option in `options`. 185 186 </section> 187 188 <!-- /.usage --> 189 190 * * * 191 192 <section class="notes"> 193 194 ## Notes 195 196 - In binary mode, a provided `value` must be a `string`, `Buffer`, or `Uint8Array`. 197 - In [`objectMode`][object-mode], `null` is a reserved value. If provided `null`, the returned [stream][stream] will prematurely end. 198 - If provided an `object` reference, the `value` is **not** copied. To avoid unwanted mutation, copy the `value` **before** creating a [stream][stream]. 199 - In older Node.js environments, `Uint8Array` contents may be copied to a new `Buffer` object due to changes in the underlying Node.js APIs. 200 - If the `factory` method is provided only one argument and that argument is an `object` (either empty or not containing any recognized `options` properties), the method treats the argument as a value to be streamed, **not** as an `options` argument. 201 202 </section> 203 204 <!-- /.notes --> 205 206 * * * 207 208 <section class="examples"> 209 210 ## Examples 211 212 <!-- eslint no-undef: "error" --> 213 214 ```javascript 215 var inspectStream = require( '@stdlib/streams/node/inspect-sink' ); 216 var constantStream = require( '@stdlib/streams/node/from-constant' ); 217 218 function log( v ) { 219 console.log( v.toString() ); 220 } 221 222 var opts = { 223 'objectMode': true, 224 'iter': 10 225 }; 226 227 var stream = constantStream( 3.14, opts ); 228 229 opts = { 230 'objectMode': true 231 }; 232 var iStream = inspectStream( opts, log ); 233 234 stream.pipe( iStream ); 235 ``` 236 237 </section> 238 239 <!-- /.examples --> 240 241 <!-- Section for describing a command-line interface. --> 242 243 * * * 244 245 <section class="cli"> 246 247 ## CLI 248 249 <!-- CLI usage documentation. --> 250 251 <section class="usage"> 252 253 ### Usage 254 255 ```text 256 Usage: constant-stream [options] <value> 257 258 Options: 259 260 -h, --help Print this message. 261 -V, --version Print the package version. 262 --sep sep Separator used to join streamed data. Default: '\n'. 263 -n, --iter iterations Number of iterations. 264 ``` 265 266 </section> 267 268 <!-- /.usage --> 269 270 <!-- CLI usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. --> 271 272 <section class="notes"> 273 274 ### Notes 275 276 - In accordance with POSIX convention, a trailing newline is **always** appended to generated output prior to exit. 277 278 </section> 279 280 <!-- /.notes --> 281 282 <!-- CLI usage examples. --> 283 284 <section class="examples"> 285 286 ### Examples 287 288 ```bash 289 $ constant-stream 'beep' -n 10 290 ``` 291 292 </section> 293 294 <!-- /.examples --> 295 296 </section> 297 298 <!-- /.cli --> 299 300 <section class="links"> 301 302 [stream]: https://nodejs.org/api/stream.html 303 304 [object-mode]: https://nodejs.org/api/stream.html#stream_object_mode 305 306 [readable-stream]: https://nodejs.org/api/stream.html 307 308 </section> 309 310 <!-- /.links -->