Automatically migrated from Gitolite
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

85 lines
2.5 KiB

7 years ago
  1. through2-spy
  2. ============
  3. [![NPM](https://nodei.co/npm/through2-sink.png)](https://nodei.co/npm/through2-sink/)
  4. This is a super thin wrapper around [through2](http://npm.im/through2) for creating simple 'terminus' streams, that do nothing but look at the chunk received.
  5. This module is heavily based on Bryce B. Baril's [through2-spy](https://www.npmjs.com/package/through2-spy), with the `.push` call removed. Why? So that you can have the same functionality at the *end* of a pipeline, without buffering everything up. It was originally written to help implement progress events in [bhttp](https://www.npmjs.com/package/bhttp). If you were to just use `through2-spy` for this, then [this](https://github.com/joyent/node/issues/14477) would happen.
  6. Pass a function to run as each chunk goes through your stream pipeline. Return an Error to abort the pipeline.
  7. ```js
  8. var sink = require("through2-sink")
  9. var count = 0
  10. var countChunks = sink(function (chunk) {
  11. count++
  12. })
  13. // vs. with through2:
  14. var countChunks = through2(function (chunk, encoding, callback) {
  15. count++
  16. return callback()
  17. })
  18. // Then use your sink:
  19. source.pipe(countChunks)
  20. // Additionally accepts `wantStrings` argument to conver buffers into strings
  21. var nsaregex = /(open source)|(foss)|(node\.js)|(mods are asleep)|(post sinks)/i
  22. var prizm = sink({wantStrings: true}, function (str) {
  23. var buggyWiretap = str.match(nsaregex)
  24. if (buggyWiretap) {
  25. this.emit("OMGTERRIST", buggyWiretap[0], str)
  26. }
  27. })
  28. prizm.on("OMGTERRIST", sendDrone(/* ... */))
  29. internet.pipe(prizm) // can't have the terrists getting their packets!
  30. // Return an Error to abort the pipeline
  31. var Meter = sink.ctor({maxBytes: 1024, bytes: 0}, function (chunk) {
  32. this.options.bytes += chunk.length
  33. if (this.options.bytes >= this.options.maxBytes) return new Error("Over 1024 byte limit!")
  34. })
  35. var meter = new Meter()
  36. ```
  37. API
  38. ---
  39. `require("through2-sink")([options], fn)`
  40. ---
  41. Create a `through2-sink` instance that will call `fn(chunk)` and then silently pass through data downstream.
  42. `require("through2-sink").ctor([options], fn)`
  43. ---
  44. Create a `through2-sink` Type that can be instantiated via `new Type()` or `Type()` to create reusable sinks.
  45. `require("through2-sink").obj([options], fn)`
  46. ---
  47. Create a `through2-sink` that defaults to `objectMode = true`.
  48. `require("through2-sink").objCtor([options], fn)`
  49. ---
  50. Create a `through2-sink` Type that defaults to `objectMode = true`.
  51. Options
  52. -------
  53. * wantStrings: Automatically call chunk.toString() for the super lazy.
  54. * all other through2 options
  55. LICENSE
  56. =======
  57. MIT