@@ -550,8 +550,7 @@ added: v9.3.0
550
550
551
551
* {number}
552
552
553
- Return the value of ` highWaterMark ` passed when constructing this
554
- ` Writable ` .
553
+ Return the value of ` highWaterMark ` passed when creating this ` Writable ` .
555
554
556
555
##### ` writable.writableLength `
557
556
<!-- YAML
@@ -1193,8 +1192,7 @@ added: v9.3.0
1193
1192
1194
1193
* {number}
1195
1194
1196
- Returns the value of ` highWaterMark ` passed when constructing this
1197
- ` Readable ` .
1195
+ Returns the value of ` highWaterMark ` passed when creating this ` Readable ` .
1198
1196
1199
1197
##### ` readable.readableLength `
1200
1198
<!-- YAML
@@ -1792,7 +1790,7 @@ expectations.
1792
1790
added: v1.2.0
1793
1791
-->
1794
1792
1795
- For many simple cases, it is possible to construct a stream without relying on
1793
+ For many simple cases, it is possible to create a stream without relying on
1796
1794
inheritance. This can be accomplished by directly creating instances of the
1797
1795
` stream.Writable ` , ` stream.Readable ` , ` stream.Duplex ` or ` stream.Transform `
1798
1796
objects and passing appropriate methods as constructor options.
@@ -1801,8 +1799,14 @@ objects and passing appropriate methods as constructor options.
1801
1799
const { Writable } = require (' stream' );
1802
1800
1803
1801
const myWritable = new Writable ({
1802
+ construct (callback ) {
1803
+ // Initialize state and load resources...
1804
+ },
1804
1805
write (chunk , encoding , callback ) {
1805
1806
// ...
1807
+ },
1808
+ destroy () {
1809
+ // Free resources...
1806
1810
}
1807
1811
});
1808
1812
```
@@ -1861,6 +1865,8 @@ changes:
1861
1865
[ ` stream._destroy() ` ] [ writable-_destroy ] method.
1862
1866
* ` final ` {Function} Implementation for the
1863
1867
[ ` stream._final() ` ] [ stream-_final ] method.
1868
+ * ` construct ` {Function} Implementation for the
1869
+ [ ` stream._construct() ` ] [ writable-_construct ] method.
1864
1870
* ` autoDestroy ` {boolean} Whether this stream should automatically call
1865
1871
` .destroy() ` on itself after ending. ** Default:** ` true ` .
1866
1872
@@ -1906,6 +1912,56 @@ const myWritable = new Writable({
1906
1912
});
1907
1913
```
1908
1914
1915
+ #### ` writable._construct(callback) `
1916
+ <!-- YAML
1917
+ added: REPLACEME
1918
+ -->
1919
+
1920
+ * ` callback ` {Function} Call this function (optionally with an error
1921
+ argument) when the stream has finished initializing.
1922
+
1923
+ The ` _construct() ` method MUST NOT be called directly. It may be implemented
1924
+ by child classes, and if so, will be called by the internal ` Writable `
1925
+ class methods only.
1926
+
1927
+ This optional function will be called in a tick after the stream constructor
1928
+ has returned, delaying any ` _write ` , ` _final ` and ` _destroy ` calls until
1929
+ ` callback ` is called. This is useful to initialize state or asynchronously
1930
+ initialize resources before the stream can be used.
1931
+
1932
+ ``` js
1933
+ const { Writable } = require (' stream' );
1934
+ const fs = require (' fs' );
1935
+
1936
+ class WriteStream extends Writable {
1937
+ constructor (filename ) {
1938
+ super ();
1939
+ this .filename = filename;
1940
+ this .fd = fd;
1941
+ }
1942
+ _construct (callback ) {
1943
+ fs .open (this .filename , (fd , err ) => {
1944
+ if (err) {
1945
+ callback (err);
1946
+ } else {
1947
+ this .fd = fd;
1948
+ callback ();
1949
+ }
1950
+ });
1951
+ }
1952
+ _write (chunk , encoding , callback ) {
1953
+ fs .write (this .fd , chunk, callback);
1954
+ }
1955
+ _destroy (err , callback ) {
1956
+ if (this .fd ) {
1957
+ fs .close (this .fd , (er ) => callback (er || err));
1958
+ } else {
1959
+ callback (err);
1960
+ }
1961
+ }
1962
+ }
1963
+ ```
1964
+
1909
1965
#### ` writable._write(chunk, encoding, callback) `
1910
1966
<!-- YAML
1911
1967
changes:
@@ -2130,6 +2186,8 @@ changes:
2130
2186
method.
2131
2187
* ` destroy ` {Function} Implementation for the
2132
2188
[ ` stream._destroy() ` ] [ readable-_destroy ] method.
2189
+ * ` construct ` {Function} Implementation for the
2190
+ [ ` stream._construct() ` ] [ readable-_construct ] method.
2133
2191
* ` autoDestroy ` {boolean} Whether this stream should automatically call
2134
2192
` .destroy() ` on itself after ending. ** Default:** ` true ` .
2135
2193
@@ -2172,6 +2230,63 @@ const myReadable = new Readable({
2172
2230
});
2173
2231
```
2174
2232
2233
+ #### ` readable._construct(callback) `
2234
+ <!-- YAML
2235
+ added: REPLACEME
2236
+ -->
2237
+
2238
+ * ` callback ` {Function} Call this function (optionally with an error
2239
+ argument) when the stream has finished initializing.
2240
+
2241
+ The ` _construct() ` method MUST NOT be called directly. It may be implemented
2242
+ by child classes, and if so, will be called by the internal ` Readable `
2243
+ class methods only.
2244
+
2245
+ This optional function will be called by the stream constructor,
2246
+ delaying any ` _read ` and ` _destroy ` calls until ` callback ` is called. This is
2247
+ useful to initialize state or asynchronously initialize resources before the
2248
+ stream can be used.
2249
+
2250
+ ``` js
2251
+ const { Readable } = require (' stream' );
2252
+ const fs = require (' fs' );
2253
+
2254
+ class ReadStream extends Readable {
2255
+ constructor (filename ) {
2256
+ super ();
2257
+ this .filename = filename;
2258
+ this .fd = null ;
2259
+ }
2260
+ _construct (callback ) {
2261
+ fs .open (this .filename , (fd , err ) => {
2262
+ if (err) {
2263
+ callback (err);
2264
+ } else {
2265
+ this .fd = fd;
2266
+ callback ();
2267
+ }
2268
+ });
2269
+ }
2270
+ _read (n ) {
2271
+ const buf = Buffer .alloc (n);
2272
+ fs .read (this .fd , buf, 0 , n, null , (err , bytesRead ) => {
2273
+ if (err) {
2274
+ this .destroy (err);
2275
+ } else {
2276
+ this .push (bytesRead > 0 ? buf .slice (0 , bytesRead) : null );
2277
+ }
2278
+ });
2279
+ }
2280
+ _destroy (err , callback ) {
2281
+ if (this .fd ) {
2282
+ fs .close (this .fd , (er ) => callback (er || err));
2283
+ } else {
2284
+ callback (err);
2285
+ }
2286
+ }
2287
+ }
2288
+ ```
2289
+
2175
2290
#### ` readable._read(size) `
2176
2291
<!-- YAML
2177
2292
added: v0.9.4
@@ -2427,6 +2542,46 @@ const myDuplex = new Duplex({
2427
2542
});
2428
2543
```
2429
2544
2545
+ When using pipeline:
2546
+
2547
+ ``` js
2548
+ const { Transform , pipeline } = require (' stream' );
2549
+ const fs = require (' fs' );
2550
+
2551
+ pipeline (
2552
+ fs .createReadStream (' object.json' )
2553
+ .setEncoding (' utf-8' ),
2554
+ new Transform ({
2555
+ decodeStrings: false , // Accept string input rather than Buffers
2556
+ construct (callback ) {
2557
+ this .data = ' ' ;
2558
+ callback ();
2559
+ },
2560
+ transform (chunk , encoding , callback ) {
2561
+ this .data += chunk;
2562
+ callback ();
2563
+ },
2564
+ flush (callback ) {
2565
+ try {
2566
+ // Make sure is valid json.
2567
+ JSON .parse (this .data );
2568
+ this .push (this .data );
2569
+ } catch (err) {
2570
+ callback (err);
2571
+ }
2572
+ }
2573
+ }),
2574
+ fs .createWriteStream (' valid-object.json' ),
2575
+ (err ) => {
2576
+ if (err) {
2577
+ console .error (' failed' , err);
2578
+ } else {
2579
+ console .log (' completed' );
2580
+ }
2581
+ }
2582
+ );
2583
+ ```
2584
+
2430
2585
#### An Example Duplex Stream
2431
2586
2432
2587
The following illustrates a simple example of a ` Duplex ` stream that wraps a
@@ -2706,8 +2861,8 @@ unhandled post-destroy errors.
2706
2861
2707
2862
#### Creating Readable Streams with Async Generators
2708
2863
2709
- We can construct a Node.js Readable Stream from an asynchronous generator
2710
- using the ` Readable.from() ` utility method:
2864
+ A Node.js Readable Stream can be created from an asynchronous generator using
2865
+ the ` Readable.from() ` utility method:
2711
2866
2712
2867
``` js
2713
2868
const { Readable } = require (' stream' );
@@ -2960,6 +3115,7 @@ contain multi-byte characters.
2960
3115
[ http-incoming-message ] : http.html#http_class_http_incomingmessage
2961
3116
[ hwm-gotcha ] : #stream_highwatermark_discrepancy_after_calling_readable_setencoding
2962
3117
[ object-mode ] : #stream_object_mode
3118
+ [ readable-_construct ] : #stream_readable_construct_callback
2963
3119
[ readable-_destroy ] : #stream_readable_destroy_err_callback
2964
3120
[ readable-destroy ] : #stream_readable_destroy_error
2965
3121
[ stream-_final ] : #stream_writable_final_callback
@@ -2976,6 +3132,7 @@ contain multi-byte characters.
2976
3132
[ stream-uncork ] : #stream_writable_uncork
2977
3133
[ stream-write ] : #stream_writable_write_chunk_encoding_callback
2978
3134
[ Stream Three States ] : #stream_three_states
3135
+ [ writable-_construct ] : #stream_writable_construct_callback
2979
3136
[ writable-_destroy ] : #stream_writable_destroy_err_callback
2980
3137
[ writable-destroy ] : #stream_writable_destroy_error
2981
3138
[ writable-new ] : #stream_constructor_new_stream_writable_options
0 commit comments