Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Optional specific stream to unpipe
- The method detaches a stream previously attached using the method.
- If the is not specified, then all pipes are detached.
- If the is specified, but no pipe is set up for it, then the method does nothing.
- All the data from readable goes into but only for the first second
- Chunk of data to unshift onto the read queue.
- For streams not operating in object mode, must be a string,
- For object mode streams, may be any JavaScript value other than
- The method pushes a chunk of data back into the internal buffer.
- This is useful in certain situations where a stream is being consumed by code that needs to "un-consume" some amount of data that it has optimistically pulled out of the source, so that the data can be passed on to some other party.
- The method cannot be called after the event has been emitted or a runtime error will be thrown.
- Developers using often should consider switching to use of a stream instead.
- See the API for Stream Implementers section for more information.
- Pull off a header delimited by
- use if we get too much
- Call the callback with
- found the header boundary
- remove the listener before unshifting
- now the body of the message can be read from the stream.
- still reading the header.
- Unlike, will not end the reading process by resetting the internal reading state of the stream.
- This can cause unexpected results if is called during a read (i.e. from within a implementation on a custom stream).
- Following the call to with an immediate will reset the reading state appropriately, however it is best to simply avoid calling while in the process of performing a read.
- An "old style" readable stream
- Versions of Node.js prior to v0.10 had streams that did not implement the entire module API as it is currently defined.
- (See Compatibility for more information.)
- When using an older Node.js library that emits events and has a method that is advisory only, the method can be used to create a stream that uses the old stream as its data source.
- It will rarely be necessary to use but the method has been provided as a convenience for interacting with older Node.js applications and libraries.
- Stability: 1 - Experimental
- to fully consume the stream.
- If the loop terminates with a or a, the stream will be destroyed.
- In other terms, iterating over a stream will consume the stream fully.
- The stream will be read in chunks of size equal to the option.
- In the code example above, data will be in a single chunk if the file has less then 64kb of data because no option is provided to
- Duplex and Transform Streams
- Duplex streams are streams that implement both the and interfaces.
- Examples of streams include:
- Transform streams are streams where the output is in some way related to the input.
- Like all streams, streams implement both the and interfaces.
- Destroy the stream, and emit
- After this call, the transform stream would release any internal resources.
- implementors should not override this method, but instead implement
- The default implementation of for also emit
- A and/or stream
- A callback function that takes an optional error argument.
- A function to get notified when a stream is no longer readable, writable or has experienced an error or a premature close event.
- drain the stream
- Especially useful in error handling scenarios where a stream is destroyed prematurely (like an aborted HTTP request), and will not emit or
- The API is promisify'able as well;
- Two or more streams to pipe between.
- A module method to pipe between streams forwarding errors and properly cleaning up and provide a callback when the pipeline is complete.
- Use the pipeline API to easily pipe a series of streams together and get notified when the pipeline is fully done.
- A pipeline to gzip a potentially huge tar file efficiently:
- API for Stream Implementers
- The module API has been designed to make it possible to easily implement streams using JavaScript's prototypal inheritance model.
- First, a stream developer would declare a new JavaScript class that extends one of the four basic stream classes ( or ), making sure they call the appropriate parent class constructor:
- The new stream class must then implement one or more specific methods, depending on the type of stream being created, as detailed in the chart below:
- Use-case
- Class
- Method(s) to implement
- The implementation code for a stream should never call the "public" methods of a stream that are intended for use by consumers (as described in the section).
- Doing so may lead to adverse side effects in application code consuming the stream.
- Simplified Construction
- For many simple cases, it is possible to construct a stream without relying on inheritance.
- This can be accomplished by directly creating instances of the or objects and passing appropriate methods as constructor options.
- Implementing a Writable Stream
- The class is extended to implement a stream.
- Custom streams must call the constructor and implement the method.
- The method may also be implemented.
- Buffer level when starts returning
- Whether or not to decode strings into before passing them to
- Whether or not the is a valid operation.
- When set, it becomes possible to write JavaScript values other than string, or if supported by the stream implementation.
- Whether or not the stream should emit after it has been destroyed.
- Implementation for the method
- Calls the constructor
- Or, when using pre-ES6 style constructors:
- Or, using the Simplified Constructor approach:
- The chunk to be written.
- Will always be a buffer unless the option was set to or the stream is operating in object mode.
- If the chunk is a string, then is the character encoding of that string.
- If chunk is a, or if the stream is operating in object mode, may be ignored.
- Call this function (optionally with an error argument) when processing is complete for the supplied chunk.
- All stream implementations must provide a method to send data to the underlying resource.
- streams provide their own implementation of the
- This function MUST NOT be called by application code directly.
- It should be implemented by child classes, and called by the internal class methods only.
- The method must be called to signal either that the write completed successfully or failed with an error.
- The first argument passed to the must be the object if the call failed or if the write succeeded.
- All calls to that occur between the time is called and the is called will cause the written data to be buffered.
- When the is invoked, the stream might emit a event.
- If a stream implementation is capable of processing multiple chunks of data at once, the method should be implemented.
- If the property is explicitly set to in the constructor options, then will remain the same object that is passed to and may be a string rather than a
- This is to support implementations that have an optimized handling for certain string data encodings.
- In that case, the argument will indicate the character encoding of the string.
- Otherwise, the argument can be safely ignored.
- The method is prefixed with an underscore because it is internal to the class that defines it, and should never be called directly by user programs.
- Each chunk has following format:
- A callback function (optionally with an error argument) to be invoked when processing is complete for the supplied chunks.
- The method may be implemented in addition to in stream implementations that are capable of processing multiple chunks of data at once.
- If implemented, the method will be called with all chunks of data currently buffered in the write queue.
- The method is called by
- It can be overridden by child classes but it must not be called directly.
- It may be implemented by child classes, and if so, will be called by the internal class methods only.
- This optional function will be called before the stream closes, delaying the event until is called.
- This is useful to close resources or write buffered data before a stream ends.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement