Skip to content

Commit 50daee7

Browse files
sonewmanchrisdickinson
authored andcommitted
stream: simpler stream constructon
Adds simplified constructor pattern, allowing users to provide "read", "write", "transform", "flush", and "writev" functions as stream options in lieu of subclassing. Semver: minor PR-URL: #697 Fixes: nodejs/readable-stream#102 Reviewed-By: Chris Dickinson <[email protected]>
1 parent e0730ee commit 50daee7

7 files changed

+175
-2
lines changed

doc/api/stream.markdown

+73-2
Original file line numberDiff line numberDiff line change
@@ -718,7 +718,7 @@ of stream class you are writing:
718718
<p>[Writable](#stream_class_stream_writable_1)</p>
719719
</td>
720720
<td>
721-
<p><code>[_write][]</code></p>
721+
<p><code>[_write][]</code>, <code>_writev</code></p>
722722
</td>
723723
</tr>
724724
<tr>
@@ -729,7 +729,7 @@ of stream class you are writing:
729729
<p>[Duplex](#stream_class_stream_duplex_1)</p>
730730
</td>
731731
<td>
732-
<p><code>[_read][]</code>, <code>[_write][]</code></p>
732+
<p><code>[_read][]</code>, <code>[_write][]</code>, <code>_writev</code></p>
733733
</td>
734734
</tr>
735735
<tr>
@@ -1315,6 +1315,77 @@ for examples and testing, but there are occasionally use cases where
13151315
it can come in handy as a building block for novel sorts of streams.
13161316

13171317

1318+
## Simplified Constructor API
1319+
1320+
<!--type=misc-->
1321+
1322+
In simple cases there is now the added benefit of being able to construct a stream without inheritance.
1323+
1324+
This can be done by passing the appropriate methods as constructor options:
1325+
1326+
Examples:
1327+
1328+
### Readable
1329+
```javascript
1330+
var readable = new stream.Readable({
1331+
read: function(n) {
1332+
// sets this._read under the hood
1333+
}
1334+
});
1335+
```
1336+
1337+
### Writable
1338+
```javascript
1339+
var writable = new stream.Writable({
1340+
write: function(chunk, encoding, next) {
1341+
// sets this._write under the hood
1342+
}
1343+
});
1344+
1345+
// or
1346+
1347+
var writable = new stream.Writable({
1348+
writev: function(chunks, next) {
1349+
// sets this._writev under the hood
1350+
}
1351+
});
1352+
```
1353+
1354+
### Duplex
1355+
```javascript
1356+
var duplex = new stream.Duplex({
1357+
read: function(n) {
1358+
// sets this._read under the hood
1359+
},
1360+
write: function(chunk, encoding, next) {
1361+
// sets this._write under the hood
1362+
}
1363+
});
1364+
1365+
// or
1366+
1367+
var duplex = new stream.Duplex({
1368+
read: function(n) {
1369+
// sets this._read under the hood
1370+
},
1371+
writev: function(chunks, next) {
1372+
// sets this._writev under the hood
1373+
}
1374+
});
1375+
```
1376+
1377+
### Transform
1378+
```javascript
1379+
var transform = new stream.Transform({
1380+
transform: function(chunk, encoding, next) {
1381+
// sets this._transform under the hood
1382+
},
1383+
flush: function(done) {
1384+
// sets this._flush under the hood
1385+
}
1386+
});
1387+
```
1388+
13181389
## Streams: Under the Hood
13191390

13201391
<!--type=misc-->

lib/_stream_readable.js

+3
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,9 @@ function Readable(options) {
8585
// legacy
8686
this.readable = true;
8787

88+
if (options && typeof options.read === 'function')
89+
this._read = options.read;
90+
8891
Stream.call(this);
8992
}
9093

lib/_stream_transform.js

+8
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,14 @@ function Transform(options) {
105105
// sync guard flag.
106106
this._readableState.sync = false;
107107

108+
if (options) {
109+
if (typeof options.transform === 'function')
110+
this._transform = options.transform;
111+
112+
if (typeof options.flush === 'function')
113+
this._flush = options.flush;
114+
}
115+
108116
this.once('prefinish', function() {
109117
if (typeof this._flush === 'function')
110118
this._flush(function(er) {

lib/_stream_writable.js

+8
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,14 @@ function Writable(options) {
137137
// legacy.
138138
this.writable = true;
139139

140+
if (options) {
141+
if (typeof options.write === 'function')
142+
this._write = options.write;
143+
144+
if (typeof options.writev === 'function')
145+
this._writev = options.writev;
146+
}
147+
140148
Stream.call(this);
141149
}
142150

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
var common = require('../common');
2+
var assert = require('assert');
3+
4+
var Readable = require('stream').Readable;
5+
6+
var _readCalled = false;
7+
function _read(n) {
8+
_readCalled = true;
9+
this.push(null);
10+
}
11+
12+
var r = new Readable({ read: _read });
13+
r.resume();
14+
15+
process.on('exit', function () {
16+
assert.equal(r._read, _read);
17+
assert(_readCalled);
18+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
var common = require('../common');
2+
var assert = require('assert');
3+
4+
var Transform = require('stream').Transform;
5+
6+
var _transformCalled = false;
7+
function _transform(d, e, n) {
8+
_transformCalled = true;
9+
n();
10+
}
11+
12+
var _flushCalled = false;
13+
function _flush(n) {
14+
_flushCalled = true;
15+
n();
16+
}
17+
18+
var t = new Transform({
19+
transform: _transform,
20+
flush: _flush
21+
});
22+
23+
t.end(new Buffer('blerg'));
24+
t.resume();
25+
26+
process.on('exit', function () {
27+
assert.equal(t._transform, _transform);
28+
assert.equal(t._flush, _flush);
29+
assert(_transformCalled);
30+
assert(_flushCalled);
31+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
var common = require('../common');
2+
var assert = require('assert');
3+
4+
var Writable = require('stream').Writable;
5+
6+
var _writeCalled = false;
7+
function _write(d, e, n) {
8+
_writeCalled = true;
9+
}
10+
11+
var w = new Writable({ write: _write });
12+
w.end(new Buffer('blerg'));
13+
14+
var _writevCalled = false;
15+
var dLength = 0;
16+
function _writev(d, n) {
17+
dLength = d.length;
18+
_writevCalled = true;
19+
}
20+
21+
var w2 = new Writable({ writev: _writev });
22+
w2.cork();
23+
24+
w2.write(new Buffer('blerg'));
25+
w2.write(new Buffer('blerg'));
26+
w2.end();
27+
28+
process.on('exit', function () {
29+
assert.equal(w._write, _write);
30+
assert(_writeCalled);
31+
assert.equal(w2._writev, _writev);
32+
assert.equal(dLength, 2);
33+
assert(_writevCalled);
34+
});

0 commit comments

Comments
 (0)