Readable to Simple stream converter
This project is an util for converting node readable streams to simple-stream format suggested by Tim Caswell, as well as more strict and detailed version of original spec.
http file server
var Simple = require'stream-simple'var http = require'http'var fs = require'fs'httpcreateServervar stream = SimplefscreateReadStreamrequrlslice1reson'error'streamabortreswriteHead200'Content-Type': 'text/plain';streamreadif err return resdestroyif !chunk return resendreswritechunkpush
npm install stream-simple
Simple stream is an object with two methods
.read() is used for pulling data from stream. It accepts callback function with
node's standard signature
cb(err, item). The following rules apply:
item !== undefined
erris an instance of
.abort() method is a signal to release all underlying resources.
It accepts optional callback function with
The following rules apply:
###Why not to support .read(n, cb)?
The last argument requires some clarification. There are types of streams (like file system streams) which indeed can produce certain amount of data and optimize on that. However in any system there are high-low watermark levels so that if you pull data in chunks beyound that size boundary you get either protocol or memory management overhead. For all modern systems I am personally aware of the typical size of "optimal" chunk is much larger than a typical size of protocol headers. Even if some parser currently wants only 100b it's almost always better to pull 10kb in advance. On other hand there is no much overhead in pulling 10kb instead of 100b as well as pulling 100kb in 10kb chunks. Also that "optimal" chunk size is known at the moment of stream construction and there is no need to pass it from outside.
Finally if some stream want's to support reading of certain amount of bytes it may do so, however general purpose APIs should not rely on that.
###We taught that callbacks should always be called asynchronously
process.nextTickand even on node it can be performance overhead
reduce) and not bother developer at all.