@@ -382,7 +382,7 @@ added: v8.0.0
382
382
changes:
383
383
- version: v14.0.0
384
384
pr-url: https://github.com/nodejs/node/pull/29197
385
- description: Work as noop when called on an already `destroyed` stream .
385
+ description: Work as a no-op on a stream that has already been destroyed .
386
386
-->
387
387
388
388
* ` error ` {Error} Optional, an error to emit with ` 'error' ` event.
@@ -397,8 +397,8 @@ This is a destructive and immediate way to destroy a stream. Previous calls to
397
397
Use ` end() ` instead of destroy if data should flush before close, or wait for
398
398
the ` 'drain' ` event before destroying the stream.
399
399
400
- Once ` destroy() ` has been called any further calls will be a noop and no
401
- further errors except from ` _destroy ` may be emitted as ` 'error' ` .
400
+ Once ` destroy() ` has been called any further calls will be a no-op and no
401
+ further errors except from ` _destroy() ` may be emitted as ` 'error' ` .
402
402
403
403
Implementors should not override this method,
404
404
but instead implement [ ` writable._destroy() ` ] [ writable-_destroy ] .
@@ -968,7 +968,7 @@ added: v8.0.0
968
968
changes:
969
969
- version: v14.0.0
970
970
pr-url: https://github.com/nodejs/node/pull/29197
971
- description: Work as noop when called on an already `destroyed` stream .
971
+ description: Work as a no-op on a stream that has already been destroyed .
972
972
-->
973
973
974
974
* ` error ` {Error} Error which will be passed as payload in ` 'error' ` event
@@ -979,8 +979,8 @@ event (unless `emitClose` is set to `false`). After this call, the readable
979
979
stream will release any internal resources and subsequent calls to ` push() `
980
980
will be ignored.
981
981
982
- Once ` destroy() ` has been called any further calls will be a noop and no
983
- further errors except from ` _destroy ` may be emitted as ` 'error' ` .
982
+ Once ` destroy() ` has been called any further calls will be a no-op and no
983
+ further errors except from ` _destroy() ` may be emitted as ` 'error' ` .
984
984
985
985
Implementors should not override this method, but instead implement
986
986
[ ` readable._destroy() ` ] [ readable-_destroy ] .
@@ -1536,7 +1536,7 @@ added: v8.0.0
1536
1536
changes:
1537
1537
- version: v14.0.0
1538
1538
pr-url: https://github.com/nodejs/node/pull/29197
1539
- description: Work as noop when called on an already `destroyed` stream .
1539
+ description: Work as a no-op on a stream that has already been destroyed .
1540
1540
-->
1541
1541
1542
1542
* ` error ` {Error}
@@ -1549,8 +1549,8 @@ Implementors should not override this method, but instead implement
1549
1549
The default implementation of ` _destroy() ` for ` Transform ` also emit ` 'close' `
1550
1550
unless ` emitClose ` is set in false.
1551
1551
1552
- Once ` destroy() ` has been called any further calls will be a noop and no
1553
- further errors except from ` _destroy ` may be emitted as ` 'error' ` .
1552
+ Once ` destroy() ` has been called, any further calls will be a no-op and no
1553
+ further errors except from ` _destroy() ` may be emitted as ` 'error' ` .
1554
1554
1555
1555
### ` stream.finished(stream[, options], callback) `
1556
1556
<!-- YAML
@@ -1953,6 +1953,56 @@ const myWritable = new Writable({
1953
1953
});
1954
1954
```
1955
1955
1956
+ #### ` writable._construct(callback) `
1957
+ <!-- YAML
1958
+ added: REPLACEME
1959
+ -->
1960
+
1961
+ * ` callback ` {Function} Call this function (optionally with an error
1962
+ argument) when the stream has finished initializing.
1963
+
1964
+ The ` _construct() ` method MUST NOT be called directly. It may be implemented
1965
+ by child classes, and if so, will be called by the internal ` Writable `
1966
+ class methods only.
1967
+
1968
+ This optional function will be called in a tick after the stream constructor
1969
+ has returned, delaying any ` _write() ` , ` _final() ` and ` _destroy() ` calls until
1970
+ ` callback ` is called. This is useful to initialize state or asynchronously
1971
+ initialize resources before the stream can be used.
1972
+
1973
+ ``` js
1974
+ const { Writable } = require (' stream' );
1975
+ const fs = require (' fs' );
1976
+
1977
+ class WriteStream extends Writable {
1978
+ constructor (filename ) {
1979
+ super ();
1980
+ this .filename = filename;
1981
+ this .fd = fd;
1982
+ }
1983
+ _construct (callback ) {
1984
+ fs .open (this .filename , (fd , err ) => {
1985
+ if (err) {
1986
+ callback (err);
1987
+ } else {
1988
+ this .fd = fd;
1989
+ callback ();
1990
+ }
1991
+ });
1992
+ }
1993
+ _write (chunk , encoding , callback ) {
1994
+ fs .write (this .fd , chunk, callback);
1995
+ }
1996
+ _destroy (err , callback ) {
1997
+ if (this .fd ) {
1998
+ fs .close (this .fd , (er ) => callback (er || err));
1999
+ } else {
2000
+ callback (err);
2001
+ }
2002
+ }
2003
+ }
2004
+ ```
2005
+
1956
2006
#### ` writable._write(chunk, encoding, callback) `
1957
2007
<!-- YAML
1958
2008
changes:
@@ -2219,6 +2269,63 @@ const myReadable = new Readable({
2219
2269
});
2220
2270
```
2221
2271
2272
+ #### ` readable._construct(callback) `
2273
+ <!-- YAML
2274
+ added: REPLACEME
2275
+ -->
2276
+
2277
+ * ` callback ` {Function} Call this function (optionally with an error
2278
+ argument) when the stream has finished initializing.
2279
+
2280
+ The ` _construct() ` method MUST NOT be called directly. It may be implemented
2281
+ by child classes, and if so, will be called by the internal ` Readable `
2282
+ class methods only.
2283
+
2284
+ This optional function will be scheduled in the next tick by the stream
2285
+ constructor, delaying any ` _read() ` and ` _destroy() ` calls until ` callback ` is
2286
+ called. This is useful to initialize state or asynchronously initialize
2287
+ resources before the stream can be used.
2288
+
2289
+ ``` js
2290
+ const { Readable } = require (' stream' );
2291
+ const fs = require (' fs' );
2292
+
2293
+ class ReadStream extends Readable {
2294
+ constructor (filename ) {
2295
+ super ();
2296
+ this .filename = filename;
2297
+ this .fd = null ;
2298
+ }
2299
+ _construct (callback ) {
2300
+ fs .open (this .filename , (fd , err ) => {
2301
+ if (err) {
2302
+ callback (err);
2303
+ } else {
2304
+ this .fd = fd;
2305
+ callback ();
2306
+ }
2307
+ });
2308
+ }
2309
+ _read (n ) {
2310
+ const buf = Buffer .alloc (n);
2311
+ fs .read (this .fd , buf, 0 , n, null , (err , bytesRead ) => {
2312
+ if (err) {
2313
+ this .destroy (err);
2314
+ } else {
2315
+ this .push (bytesRead > 0 ? buf .slice (0 , bytesRead) : null );
2316
+ }
2317
+ });
2318
+ }
2319
+ _destroy (err , callback ) {
2320
+ if (this .fd ) {
2321
+ fs .close (this .fd , (er ) => callback (er || err));
2322
+ } else {
2323
+ callback (err);
2324
+ }
2325
+ }
2326
+ }
2327
+ ```
2328
+
2222
2329
#### ` readable._read(size) `
2223
2330
<!-- YAML
2224
2331
added: v0.9.4
0 commit comments