# 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.
没有合适的资源?快使用搜索试试~ 我知道了~
结合html5+nodejs+express实现拖拽上传功能
共523个文件
js:208个
png:83个
md:39个
5星 · 超过95%的资源 需积分: 38 1.3k 下载量 189 浏览量
2014-09-23
19:13:50
上传
评论 10
收藏 7.44MB RAR 举报
温馨提示
文件上传是一个比较常见的功能,传统的选择方式的上传比较麻烦,需要先点击上传按钮,然后再找到文件的路径,然后上传。给用户体验带来很大问题。html5开始支持拖拽上传的需要的api。nodejs也是一个最近越来越流行的技术,这也是自己第一次接触nodejs,在nodejs开发中,最常用的开发框架之一是expess,它是一个类似mvc模式的框架。结合html5、nodejs express实现了拖拽上传的功能。
资源推荐
资源详情
资源评论
收起资源包目录
结合html5+nodejs+express实现拖拽上传功能 (523个子文件)
SwitchyOptions.bak 5KB
express.cmd 135B
style.css 3KB
upload.css 2KB
style.css 110B
style.css 31B
file.ejs 2KB
comments.ejs 272B
menu.ejs 250B
index.ejs 205B
functions.ejs 183B
list.ejs 128B
no.newlines.ejs 89B
newlines.ejs 89B
include.ejs 79B
messed.ejs 71B
error.ejs 62B
include.css.ejs 61B
double-quote.ejs 40B
item.ejs 38B
fail.ejs 34B
single-quote.ejs 33B
menu-item.ejs 32B
pet.ejs 24B
user.ejs 17B
para.ejs 10B
backslash.ejs 4B
express 9KB
express 278B
sprite_n1.gif 3KB
blank.gif 49B
.gitmodules 207B
.gitmodules 0B
binaryfile.tar.gz 301B
directory.html 2KB
client.html 588B
browser.html 436B
error.html 276B
comments.html 107B
menu.html 98B
include.html 76B
newlines.html 76B
no.newlines.html 66B
messed.html 48B
include.css.html 37B
double-quote.html 26B
single-quote.html 24B
backslash.html 4B
pf1y5.png.http 751KB
beta-sticker-1.png.http 3KB
menu_seperator.png.http 2KB
osx-chrome-13.http 1KB
osx-firefox-3.6.http 954B
xp-chrome-12.http 921B
osx-safari-5.http 898B
xp-safari-5.http 859B
binaryfile.tar.gz.http 838B
xp-ie-8.http 811B
xp-ie-7.http 798B
blank.gif.http 485B
preamble.http 353B
filename-name.http 351B
generic.http 342B
plain.txt.http 340B
crlf.http 311B
missing-hyphens2.http 308B
missing-hyphens1.http 308B
favicon.ico 1KB
FileUploader.iml 374B
Jquery优化建议.jpg 385KB
jquery-1.10.2.min.js 91KB
fs.js 42KB
index.js 25KB
_stream_readable.js 25KB
response.js 18KB
index.js 16KB
_stream_writable.js 12KB
zlib.js 12KB
test-stream2-transform.js 12KB
application.js 12KB
request.js 12KB
ejs.js 11KB
test-stream2-basic.js 10KB
session.js 10KB
index.js 10KB
send.js 10KB
test-stream2-writable.js 9KB
ejs.js 8KB
utils.js 8KB
index.js 8KB
test-stream2-set-encoding.js 8KB
ejs.js 7KB
_stream_transform.js 7KB
logger.js 7KB
index.js 7KB
test-stream2-objects.js 7KB
uploaderApp.js 7KB
utils.js 7KB
ndir.js 6KB
ejs.min.js 6KB
共 523 条
- 1
- 2
- 3
- 4
- 5
- 6
「已注销」
- 粉丝: 165
- 资源: 186
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功
- 1
- 2
前往页