# readable-stream
A new class of streams for Node.js
This module provides the new Stream base classes introduced in Node
v0.10, for use in Node v0.8. You can use it to have programs that
have to work with node v0.8, while being forward-compatible for v0.10
and beyond. When you drop support for v0.8, you can remove this
module, and only use the native streams.
This is almost exactly the same codebase as appears in Node v0.10.
However:
1. The exported object is actually the Readable class. Decorating the
native `stream` module would be global pollution.
2. In v0.10, you can safely use `base64` as an argument to
`setEncoding` in Readable streams. However, in v0.8, the
StringDecoder class has no `end()` method, which is problematic for
Base64. So, don't use that, because it'll break and be weird.
Other than that, the API is the same as `require('stream')` in v0.10,
so the API docs are reproduced below.
----------
Stability: 2 - Unstable
A stream is an abstract interface implemented by various objects in
Node. For example a request to an HTTP server is a stream, as is
stdout. Streams are readable, writable, or both. All streams are
instances of [EventEmitter][]
You can load the Stream base classes by doing `require('stream')`.
There are base classes provided for Readable streams, Writable
streams, Duplex streams, and Transform streams.
## Compatibility
In earlier versions of Node, the Readable stream interface was
simpler, but also less powerful and less useful.
* Rather than waiting for you to call the `read()` method, `'data'`
events would start emitting immediately. If you needed to do some
I/O to decide how to handle data, then you had to store the chunks
in some kind of buffer so that they would not be lost.
* The `pause()` method was advisory, rather than guaranteed. This
meant that you still had to be prepared to receive `'data'` events
even when the stream was in a paused state.
In Node v0.10, the Readable class described below was added. For
backwards compatibility with older Node programs, Readable streams
switch into "old mode" when a `'data'` event handler is added, or when
the `pause()` or `resume()` methods are called. The effect is that,
even if you are not using the new `read()` method and `'readable'`
event, you no longer have to worry about losing `'data'` chunks.
Most programs will continue to function normally. However, this
introduces an edge case in the following conditions:
* No `'data'` event handler is added.
* The `pause()` and `resume()` methods are never called.
For example, consider the following code:
```javascript
// WARNING! BROKEN!
net.createServer(function(socket) {
// we add an 'end' method, but never consume the data
socket.on('end', function() {
// It will never get here.
socket.end('I got your message (but didnt read it)\n');
});
}).listen(1337);
```
In versions of node prior to v0.10, the incoming message data would be
simply discarded. However, in Node v0.10 and beyond, the socket will
remain paused forever.
The workaround in this situation is to call the `resume()` method to
trigger "old mode" behavior:
```javascript
// Workaround
net.createServer(function(socket) {
socket.on('end', function() {
socket.end('I got your message (but didnt read it)\n');
});
// start the flow of data, discarding it.
socket.resume();
}).listen(1337);
```
In addition to new Readable streams switching into old-mode, pre-v0.10
style streams can be wrapped in a Readable class using the `wrap()`
method.
## Class: stream.Readable
<!--type=class-->
A `Readable Stream` has the following methods, members, and events.
Note that `stream.Readable` is an abstract class designed to be
extended with an underlying implementation of the `_read(size)`
method. (See below.)
### new stream.Readable([options])
* `options` {Object}
* `highWaterMark` {Number} The maximum number of bytes to store in
the internal buffer before ceasing to read from the underlying
resource. Default=16kb
* `encoding` {String} If specified, then buffers will be decoded to
strings using the specified encoding. Default=null
* `objectMode` {Boolean} Whether this stream should behave
as a stream of objects. Meaning that stream.read(n) returns
a single value instead of a Buffer of size n
In classes that extend the Readable class, make sure to call the
constructor so that the buffering settings can be properly
initialized.
### readable.\_read(size)
* `size` {Number} Number of bytes to read asynchronously
Note: **This function should NOT be called directly.** It should be
implemented by child classes, and called by the internal Readable
class methods only.
All Readable stream implementations must provide a `_read` method
to fetch data from the underlying resource.
This method is prefixed with an underscore because it is internal to
the class that defines it, and should not be called directly by user
programs. However, you **are** expected to override this method in
your own extension classes.
When data is available, put it into the read queue by calling
`readable.push(chunk)`. If `push` returns false, then you should stop
reading. When `_read` is called again, you should start pushing more
data.
The `size` argument is advisory. Implementations where a "read" is a
single call that returns data can use this to know how much data to
fetch. Implementations where that is not relevant, such as TCP or
TLS, may ignore this argument, and simply provide data whenever it
becomes available. There is no need, for example to "wait" until
`size` bytes are available before calling `stream.push(chunk)`.
### readable.push(chunk)
* `chunk` {Buffer | null | String} Chunk of data to push into the read queue
* return {Boolean} Whether or not more pushes should be performed
Note: **This function should be called by Readable implementors, NOT
by consumers of Readable subclasses.** The `_read()` function will not
be called again until at least one `push(chunk)` call is made. If no
data is available, then you MAY call `push('')` (an empty string) to
allow a future `_read` call, without adding any data to the queue.
The `Readable` class works by putting data into a read queue to be
pulled out later by calling the `read()` method when the `'readable'`
event fires.
The `push()` method will explicitly insert some data into the read
queue. If it is called with `null` then it will signal the end of the
data.
In some cases, you may be wrapping a lower-level source which has some
sort of pause/resume mechanism, and a data callback. In those cases,
you could wrap the low-level source object by doing something like
this:
```javascript
// source is an object with readStop() and readStart() methods,
// and an `ondata` member that gets called when it has data, and
// an `onend` member that gets called when the data is over.
var stream = new Readable();
source.ondata = function(chunk) {
// if push() returns false, then we need to stop reading from source
if (!stream.push(chunk))
source.readStop();
};
source.onend = function() {
stream.push(null);
};
// _read will be called when the stream wants to pull more data in
// the advisory size argument is ignored in this case.
stream._read = function(n) {
source.readStart();
};
```
### readable.unshift(chunk)
* `chunk` {Buffer | null | String} Chunk of data to unshift onto the read queue
* return {Boolean} Whether or not more pushes should be performed
This is the corollary of `readable.push(chunk)`. Rather than putting
the data at the *end* of the read queue, it puts it at the *front* of
the read queue.
This is useful in certain use-cases where a stream is being consumed
by a parser, which needs to "un-consume" some data that it has
optimistically pulled out of the source.
```javascript
// A parser for a simple data protocol.
// The "header" is a JSON object, followed by 2 \n characters, and
// then a message body.
没有合适的资源?快使用搜索试试~ 我知道了~
温馨提示
Ace 是一个轻量、功能丰富、HTML5、响应式、支持手机及平板电脑上浏览的管理后台模板,基于CSS框架Bootstrap制作,Bootstrap版本更新至 3.0,Ace – Responsive Admin Template当前最新版!资源含有全部源码和说明文档,是官方付费正式版。
资源推荐
资源详情
资源评论
收起资源包目录
ACE-Responsive Admin Template (2000个子文件)
changelog 4KB
CHANGES 1KB
CHANGES 1KB
Changes 1KB
Changes 1KB
yuicompressor.cmd 155B
yuicompressor.cmd 155B
hulk.cmd 135B
hulk.cmd 135B
CNAME 17B
CNAME 17B
ace.css 328KB
ace.css 323KB
ace.min.css 271KB
ace.min.css 265KB
bootstrap.css 124KB
bootstrap.css 116KB
bootstrap.min.css 104KB
bootstrap.min.css 94KB
ace-rtl.css 44KB
font-awesome-ie7.css 40KB
font-awesome-ie7.css 40KB
font-awesome-ie7.min.css 37KB
font-awesome-ie7.min.css 37KB
font-awesome-ie7.min.css 37KB
font-awesome-ie7.min.css 37KB
ace-rtl.min.css 36KB
ace-skins.css 32KB
ace-skins.css 31KB
font-awesome.css 27KB
font-awesome.css 27KB
ace-skins.min.css 26KB
ace-skins.min.css 26KB
docs.css 22KB
docs.css 22KB
font-awesome.min.css 22KB
bootstrap-responsive.css 22KB
font-awesome.min.css 22KB
font-awesome.min.css 22KB
font-awesome.min.css 22KB
bootstrap-editable.css 21KB
bootstrap-editable.css 21KB
ace-responsive.css 20KB
select2.css 19KB
select2.css 19KB
bootstrap-responsive.min.css 16KB
ace-responsive.min.css 15KB
chosen.css 13KB
chosen.css 13KB
chosen.css 13KB
chosen.css 13KB
ui.jqgrid.css 12KB
ui.jqgrid.css 12KB
jquery-ui-1.10.3.full.min.css 12KB
jquery-ui-1.10.3.full.min.css 12KB
dropzone.css 11KB
dropzone.css 11KB
chosen.min.css 11KB
chosen.min.css 11KB
fullcalendar.css 11KB
fullcalendar.css 11KB
datepicker.css 10KB
datepicker.css 10KB
skeleton.css 9KB
skeleton.css 9KB
ace-ie.css 8KB
ace-ie.css 8KB
ace-ie.min.css 7KB
ace-ie.min.css 6KB
layout.css 5KB
layout.css 5KB
daterangepicker.css 5KB
daterangepicker.css 5KB
qunit.css 4KB
qunit.css 4KB
qunit.css 4KB
qunit.css 4KB
colorbox.css 4KB
colorbox.css 4KB
jquery-ui-1.10.3.custom.css 4KB
jquery-ui-1.10.3.custom.css 4KB
jquery-ui-1.10.3.custom.min.css 3KB
jquery-ui-1.10.3.custom.min.css 3KB
bootstrap-timepicker.css 3KB
bootstrap-timepicker.css 3KB
colorpicker.css 2KB
colorpicker.css 2KB
jquery.gritter.css 2KB
jquery.gritter.css 2KB
prettify.css 817B
prettify.css 817B
fullcalendar.print.css 668B
fullcalendar.print.css 668B
grid.css 614B
ace-fonts.css 512B
ace-fonts.css 512B
elements.css 260B
elements.css 260B
grid.css 222B
domains2.csv 909B
共 2000 条
- 1
- 2
- 3
- 4
- 5
- 6
- 20
资源评论
fanyiboat
- 粉丝: 0
- 资源: 1
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功