|
| 1 | +/* |
| 2 | +* @license Apache-2.0 |
| 3 | +* |
| 4 | +* Copyright (c) 2021 The Stdlib Authors. |
| 5 | +* |
| 6 | +* Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 | +* you may not use this file except in compliance with the License. |
| 8 | +* You may obtain a copy of the License at |
| 9 | +* |
| 10 | +* http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | +* |
| 12 | +* Unless required by applicable law or agreed to in writing, software |
| 13 | +* distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | +* See the License for the specific language governing permissions and |
| 16 | +* limitations under the License. |
| 17 | +*/ |
| 18 | + |
| 19 | +// TypeScript Version: 2.0 |
| 20 | + |
| 21 | +/// <reference types="node"/> |
| 22 | + |
| 23 | +import { Writable } from 'stream'; |
| 24 | + |
| 25 | +/** |
| 26 | +* Interface defining stream options. |
| 27 | +*/ |
| 28 | +interface Options { |
| 29 | + /** |
| 30 | + * Specifies whether a stream should operate in object mode (default: `false`). |
| 31 | + */ |
| 32 | + objectMode?: boolean; |
| 33 | + |
| 34 | + /** |
| 35 | + * Specifies the `Buffer` level for when `write()` starts returning `false`. |
| 36 | + */ |
| 37 | + highWaterMark?: number; |
| 38 | + |
| 39 | + /** |
| 40 | + * Specifies whether to encode strings as `Buffer` objects before writing data to a returned stream (default: true). |
| 41 | + */ |
| 42 | + decodeStrings?: boolean; |
| 43 | + |
| 44 | + /** |
| 45 | + * Default encoding when not explicitly specified when writing data (default: 'utf-8'). |
| 46 | + */ |
| 47 | + defaultEncoding?: string; |
| 48 | +} |
| 49 | + |
| 50 | +/** |
| 51 | +* Callback function invoked upon receiving data. |
| 52 | +*/ |
| 53 | +type Nullary = () => void; |
| 54 | + |
| 55 | +/** |
| 56 | +* Callback function invoked upon receiving data. |
| 57 | +* |
| 58 | +* @param chunk - data chunk |
| 59 | +*/ |
| 60 | +type Unary = ( chunk: any ) => void; |
| 61 | + |
| 62 | +/** |
| 63 | +* Callback function invoked upon receiving data. |
| 64 | +* |
| 65 | +* @param chunk - data chunk |
| 66 | +* @param idx - chunk index |
| 67 | +*/ |
| 68 | +type Binary = ( chunk: any, idx: number ) => void; |
| 69 | + |
| 70 | +/** |
| 71 | +* Callback function invoked upon receiving data. |
| 72 | +* |
| 73 | +* @param chunk - data chunk |
| 74 | +* @param idx - chunk index |
| 75 | +*/ |
| 76 | +type Callback = Nullary | Unary | Binary; |
| 77 | + |
| 78 | +/** |
| 79 | +* Interface defining a stream constructor which is both "newable" and "callable". |
| 80 | +*/ |
| 81 | +interface Constructor { |
| 82 | + /** |
| 83 | + * Inspect stream constructor. |
| 84 | + * |
| 85 | + * @param options - stream options |
| 86 | + * @param options.objectMode - specifies whether the stream should operate in object mode (default: false) |
| 87 | + * @param options.highWaterMark - specifies the `Buffer` level for when `write()` starts returning `false` |
| 88 | + * @param options.decodeStrings - specifies whether to encode strings as `Buffer` objects before writing data to a returned stream (default: true) |
| 89 | + * @param options.defaultEncoding - default encoding when not explicitly specified when writing data (default: 'utf-8') |
| 90 | + * @param clbk - callback to invoke upon receiving data |
| 91 | + * @throws must provide valid options |
| 92 | + * @returns inspect stream |
| 93 | + * |
| 94 | + * @example |
| 95 | + * function log( chunk, idx ) { |
| 96 | + * console.log( 'index: %d', idx ); |
| 97 | + * console.log( chunk ); |
| 98 | + * } |
| 99 | + * |
| 100 | + * var InspectSinkStream = inspectSinkStream; |
| 101 | + * var stream = new InspectSinkStream( {}, log ); |
| 102 | + * |
| 103 | + * stream.write( 'a' ); |
| 104 | + * stream.write( 'b' ); |
| 105 | + * stream.write( 'c' ); |
| 106 | + * |
| 107 | + * stream.end(); |
| 108 | + * |
| 109 | + * // prints: index: 0 |
| 110 | + * // prints: a |
| 111 | + * // prints: index: 1 |
| 112 | + * // prints: b |
| 113 | + * // prints: index: 2 |
| 114 | + * // prints: c |
| 115 | + */ |
| 116 | + new( options: Options, clbk: Callback ): Writable; // newable |
| 117 | + |
| 118 | + /** |
| 119 | + * Inspect stream constructor. |
| 120 | + * |
| 121 | + * @param clbk - callback to invoke upon receiving data |
| 122 | + * @throws must provide valid options |
| 123 | + * @returns inspect stream |
| 124 | + * |
| 125 | + * @example |
| 126 | + * function log( chunk, idx ) { |
| 127 | + * console.log( 'index: %d', idx ); |
| 128 | + * console.log( chunk ); |
| 129 | + * } |
| 130 | + * |
| 131 | + * var InspectSinkStream = inspectSinkStream; |
| 132 | + * var stream = new InspectSinkStream( log ); |
| 133 | + * |
| 134 | + * stream.write( 'a' ); |
| 135 | + * stream.write( 'b' ); |
| 136 | + * stream.write( 'c' ); |
| 137 | + * |
| 138 | + * stream.end(); |
| 139 | + * |
| 140 | + * // prints: index: 0 |
| 141 | + * // prints: a |
| 142 | + * // prints: index: 1 |
| 143 | + * // prints: b |
| 144 | + * // prints: index: 2 |
| 145 | + * // prints: c |
| 146 | + */ |
| 147 | + new( clbk: Callback ): Writable; // newable |
| 148 | + |
| 149 | + /** |
| 150 | + * Inspect stream constructor. |
| 151 | + * |
| 152 | + * @param options - stream options |
| 153 | + * @param options.objectMode - specifies whether the stream should operate in object mode (default: false) |
| 154 | + * @param options.highWaterMark - specifies the `Buffer` level for when `write()` starts returning `false` |
| 155 | + * @param options.decodeStrings - specifies whether to encode strings as `Buffer` objects before writing data to a returned stream (default: true) |
| 156 | + * @param options.defaultEncoding - default encoding when not explicitly specified when writing data (default: 'utf-8') |
| 157 | + * @param clbk - callback to invoke upon receiving data |
| 158 | + * @throws must provide valid options |
| 159 | + * @returns inspect stream |
| 160 | + * |
| 161 | + * @example |
| 162 | + * function log( chunk, idx ) { |
| 163 | + * console.log( 'index: %d', idx ); |
| 164 | + * console.log( chunk ); |
| 165 | + * } |
| 166 | + * |
| 167 | + * var stream = inspectSinkStream( {}, log ); |
| 168 | + * |
| 169 | + * stream.write( 'a' ); |
| 170 | + * stream.write( 'b' ); |
| 171 | + * stream.write( 'c' ); |
| 172 | + * |
| 173 | + * stream.end(); |
| 174 | + * |
| 175 | + * // prints: index: 0 |
| 176 | + * // prints: a |
| 177 | + * // prints: index: 1 |
| 178 | + * // prints: b |
| 179 | + * // prints: index: 2 |
| 180 | + * // prints: c |
| 181 | + */ |
| 182 | + ( options: Options, clbk: Callback ): Writable; // callable |
| 183 | + |
| 184 | + /** |
| 185 | + * Inspect stream constructor. |
| 186 | + * |
| 187 | + * @param clbk - callback to invoke upon receiving data |
| 188 | + * @throws must provide valid options |
| 189 | + * @returns inspect stream |
| 190 | + * |
| 191 | + * @example |
| 192 | + * function log( chunk, idx ) { |
| 193 | + * console.log( 'index: %d', idx ); |
| 194 | + * console.log( chunk ); |
| 195 | + * } |
| 196 | + * |
| 197 | + * var stream = inspectSinkStream( log ); |
| 198 | + * |
| 199 | + * stream.write( 'a' ); |
| 200 | + * stream.write( 'b' ); |
| 201 | + * stream.write( 'c' ); |
| 202 | + * |
| 203 | + * stream.end(); |
| 204 | + * |
| 205 | + * // prints: index: 0 |
| 206 | + * // prints: a |
| 207 | + * // prints: index: 1 |
| 208 | + * // prints: b |
| 209 | + * // prints: index: 2 |
| 210 | + * // prints: c |
| 211 | + */ |
| 212 | + ( clbk: Callback ): Writable; // callable |
| 213 | + |
| 214 | + /** |
| 215 | + * Creates a reusable inspect stream factory. |
| 216 | + * |
| 217 | + * @param options - stream options |
| 218 | + * @param options.objectMode - specifies whether the stream should operate in object mode (default: false) |
| 219 | + * @param options.highWaterMark - specifies the `Buffer` level for when `write()` starts returning `false` |
| 220 | + * @param options.decodeStrings - specifies whether to encode strings as `Buffer` objects before writing data to a returned stream (default: true) |
| 221 | + * @param options.defaultEncoding - default encoding when not explicitly specified when writing data (default: 'utf-8') |
| 222 | + * @returns inspect stream factory |
| 223 | + * |
| 224 | + * @example |
| 225 | + * function log( chunk, idx ) { |
| 226 | + * console.log( 'index: %d', idx ); |
| 227 | + * console.log( chunk ); |
| 228 | + * } |
| 229 | + * |
| 230 | + * var opts = { |
| 231 | + * 'objectMode': true, |
| 232 | + * 'highWaterMark': 64 |
| 233 | + * }; |
| 234 | + * |
| 235 | + * var factory = inspectSinkStream.factory( opts ); |
| 236 | + * |
| 237 | + * // Create 10 identically configured streams... |
| 238 | + * var streams = []; |
| 239 | + * var i; |
| 240 | + * for ( i = 0; i < 10; i++ ) { |
| 241 | + * streams.push( factory( log ) ); |
| 242 | + * } |
| 243 | + */ |
| 244 | + factory( options?: Options ): ( clbk: Callback ) => Writable; |
| 245 | + |
| 246 | + /** |
| 247 | + * Returns an inspect stream with `objectMode` set to `true`. |
| 248 | + * |
| 249 | + * @param options - stream options |
| 250 | + * @param options.highWaterMark - specifies the `Buffer` level for when `write()` starts returning `false` |
| 251 | + * @param options.decodeStrings - specifies whether to encode strings as `Buffer` objects before writing data to a returned stream (default: true) |
| 252 | + * @param options.defaultEncoding - default encoding when not explicitly specified when writing data (default: 'utf-8') |
| 253 | + * @param clbk - callback to invoke upon receiving data |
| 254 | + * @throws must provide valid options |
| 255 | + * @returns inspect stream |
| 256 | + * |
| 257 | + * @example |
| 258 | + * function log( chunk, idx ) { |
| 259 | + * console.log( 'index: %d', idx ); |
| 260 | + * console.log( chunk ); |
| 261 | + * } |
| 262 | + * |
| 263 | + * var stream = inspectSinkStream.objectMode( {}, log ); |
| 264 | + * |
| 265 | + * stream.write( {'value': 'a'} ); |
| 266 | + * stream.write( {'value': 'b'} ); |
| 267 | + * stream.write( {'value': 'c'} ); |
| 268 | + * |
| 269 | + * stream.end(); |
| 270 | + * |
| 271 | + * // prints: 'index: 0' |
| 272 | + * // prints: {'value': 'a'} |
| 273 | + * // prints: 'index: 1' |
| 274 | + * // prints: {'value': 'b'} |
| 275 | + * // prints: 'index: 2' |
| 276 | + * // prints: {'value': 'c'} |
| 277 | + */ |
| 278 | + objectMode( options: Options, clbk: Callback ): Writable; |
| 279 | + |
| 280 | + /** |
| 281 | + * Returns an inspect stream with `objectMode` set to `true`. |
| 282 | + * |
| 283 | + * @param clbk - callback to invoke upon receiving data |
| 284 | + * @returns inspect stream |
| 285 | + * |
| 286 | + * @example |
| 287 | + * function log( chunk, idx ) { |
| 288 | + * console.log( 'index: %d', idx ); |
| 289 | + * console.log( chunk ); |
| 290 | + * } |
| 291 | + * |
| 292 | + * var stream = inspectSinkStream.objectMode( log ); |
| 293 | + * |
| 294 | + * stream.write( {'value': 'a'} ); |
| 295 | + * stream.write( {'value': 'b'} ); |
| 296 | + * stream.write( {'value': 'c'} ); |
| 297 | + * |
| 298 | + * stream.end(); |
| 299 | + * |
| 300 | + * // prints: 'index: 0' |
| 301 | + * // prints: {'value': 'a'} |
| 302 | + * // prints: 'index: 1' |
| 303 | + * // prints: {'value': 'b'} |
| 304 | + * // prints: 'index: 2' |
| 305 | + * // prints: {'value': 'c'} |
| 306 | + */ |
| 307 | + objectMode( clbk: Callback ): Writable; |
| 308 | +} |
| 309 | + |
| 310 | +/** |
| 311 | +* Returns an inspect sink stream. |
| 312 | +* |
| 313 | +* @param options - stream options |
| 314 | +* @throws must provide valid options |
| 315 | +* @returns stream instance |
| 316 | +* |
| 317 | +* @example |
| 318 | +* function log( chunk, idx ) { |
| 319 | +* console.log( 'index: %d', idx ); |
| 320 | +* console.log( chunk ); |
| 321 | +* } |
| 322 | +* |
| 323 | +* var InspectSinkStream = inspectSinkStream; |
| 324 | +* var stream = new InspectSinkStream( {}, log ); |
| 325 | +* |
| 326 | +* stream.write( 'a' ); |
| 327 | +* stream.write( 'b' ); |
| 328 | +* stream.write( 'c' ); |
| 329 | +* |
| 330 | +* stream.end(); |
| 331 | +* |
| 332 | +* // prints: index: 0 |
| 333 | +* // prints: a |
| 334 | +* // prints: index: 1 |
| 335 | +* // prints: b |
| 336 | +* // prints: index: 2 |
| 337 | +* // prints: c |
| 338 | +* |
| 339 | +* @example |
| 340 | +* function log( chunk, idx ) { |
| 341 | +* console.log( 'index: %d', idx ); |
| 342 | +* console.log( chunk ); |
| 343 | +* } |
| 344 | +* |
| 345 | +* var opts = { |
| 346 | +* 'objectMode': true, |
| 347 | +* 'highWaterMark': 64 |
| 348 | +* }; |
| 349 | +* |
| 350 | +* var factory = inspectSinkStream.factory( opts ); |
| 351 | +* |
| 352 | +* // Create 10 identically configured streams... |
| 353 | +* var streams = []; |
| 354 | +* var i; |
| 355 | +* for ( i = 0; i < 10; i++ ) { |
| 356 | +* streams.push( factory( log ) ); |
| 357 | +* } |
| 358 | +* |
| 359 | +* @example |
| 360 | +* function log( chunk, idx ) { |
| 361 | +* console.log( 'index: %d', idx ); |
| 362 | +* console.log( chunk ); |
| 363 | +* } |
| 364 | +* |
| 365 | +* var stream = inspectSinkStream.objectMode( {}, log ); |
| 366 | +* |
| 367 | +* stream.write( {'value': 'a'} ); |
| 368 | +* stream.write( {'value': 'b'} ); |
| 369 | +* stream.write( {'value': 'c'} ); |
| 370 | +* |
| 371 | +* stream.end(); |
| 372 | +* |
| 373 | +* // prints: 'index: 0' |
| 374 | +* // prints: {'value': 'a'} |
| 375 | +* // prints: 'index: 1' |
| 376 | +* // prints: {'value': 'b'} |
| 377 | +* // prints: 'index: 2' |
| 378 | +* // prints: {'value': 'c'} |
| 379 | +*/ |
| 380 | +declare var inspectSinkStream: Constructor; |
| 381 | + |
| 382 | + |
| 383 | +// EXPORTS // |
| 384 | + |
| 385 | +export = inspectSinkStream; |
0 commit comments