stream-interceptor
Tiny node.js module to intercept, modify and/or ignore chunks of data and events in any readable compatible stream before it's processed by other stream consumers (e.g: via pipe()
).
It becomes particularly useful to deal with net/http/fs streams.
Installation
npm install stream-interceptor
Examples
Existent stream
var Readable = Readablevar interceptor = // Create a new Readable streamvar stream = stream { /* ... */ } // Make it interceptable // Prepare to capture chunksstream // We gonna handle stringsstream // Push chunks to the streamstreamstreamstream // we're done! // Listen for events like a stream consumerstream stream
Capture HTTP response
var http = var interceptor = // Test servervar server = http http
Capture asynchronously
var Readable = Readablevar interceptor = // Create a new Readable streamvar stream = stream { /* ... */ } // Make it interceptable // Prepare to capture chunks asyncronously// Chunks will be processed always as FIFO queuestream // We gonna handle stringsstream // Push chunks to the streamstreamstreamstream // we're done! stream
Ignore chunks
var Readable = Readablevar interceptor = // Create a new Readable streamvar stream = stream { /* ... */ } // Make it interceptable // Prepare to capture chunksstream // We gonna handle stringsstream // Push chunks to the streamstreamstreamstreamstream // we're done! stream
Interceptable stream
var Interceptable = Interceptable // Implements both Readable and Interceptable streamvar stream = stream { /* ... */ } // Prepate to capture chunksstream // Push chunks to the streamstreamstreamstream // we're done! stream
Event interceptor
var Interceptable = Interceptable // Implements both Readable and Interceptable streamvar stream = stream { /* ... */ } // Prepate to capture eventsstream // Prepate to capture chunksstream stream // Push data in the streamstreamstreamstream // Simulate an errorstream stream
API
streamIntercept(readableStream) => Interceptable
Wraps any readable stream turning it an interceptable stream.
Interceptable
stream implements the same interface as Readable
.
Interceptable([ options ])
Alias: Interceptor
Creates a new Interceptable
stream. Inherits from Readable
stream.
boolean
Interceptable#interceptable => Property to determine if the stream is interceptable.
Interceptable
Interceptable#capture(fn) => Subscribe to capture chunks of data emitted by the stream.
Interceptable
Interceptable#captureEvent(event, fn) => Capture data emitted by a specific event.
fn
argument expects two arguments: chunk, callback
.
When you're done, you must call the callback passing the new argument: callback(chunk)
You can optionally ignore chunks passing true
to the callback: callback(true)
.
boolean
isInterceptor(stream) => Hook()
Hook layer internally used capture and handle events of the stream.
Queue()
FIFO queue implementation internally used.
Chunk()
Internal chunk event structure.
License
MIT - Tomas Aparicio