chunk_id large_stringlengths 36 36 | doc_type large_stringclasses 2
values | category large_stringclasses 17
values | document large_stringclasses 50
values | version_raw large_stringclasses 590
values | chunk_index int64 0 262 | text large_stringlengths 11 5.69k | file_path large_stringclasses 882
values |
|---|---|---|---|---|---|---|---|
4c017fd4-4aa2-5397-9047-5e702343e8ae | docs | nodejs | nodejs_stream | 8x | 15 | `Uint8Array`. For object mode streams, `chunk` may be any JavaScript value
other than `null`.
* `encoding` [<string>](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type) The encoding, if `chunk` is a string
* `callback` [<Function>](https://developer.mozilla.org/en-US/docs/Web/JavaS... | large_dataset/raw/docs/nodejs/stream/stream_8x.md |
a1734043-c6e7-5811-8ffc-bec0b0b874db | docs | nodejs | nodejs_stream | 8x | 16 | // write 'hello, ' and then end with 'world!'
const file = fs.createWriteStream('example.txt');
file.write('hello, ');
file.end('world!');
// writing more now is not allowed!
```
##### writable.setDefaultEncoding(encoding)[#](#stream_writable_setdefaultencoding_encoding)
History
| Version | Changes |
| --- | --- |
|... | large_dataset/raw/docs/nodejs/stream/stream_8x.md |
afbf371b-2add-5fbd-aa71-3bb4ce9ca32b | docs | nodejs | nodejs_stream | 8x | 17 | When using [`writable.cork()`](#stream_writable_cork) and `writable.uncork()` to manage the buffering
of writes to a stream, it is recommended that calls to `writable.uncork()` be
deferred using `process.nextTick()`. Doing so allows batching of all
`writable.write()` calls that occur within a given Node.js event loop p... | large_dataset/raw/docs/nodejs/stream/stream_8x.md |
0bb80339-aaaf-5c01-93c0-03f57d752b7e | docs | nodejs | nodejs_stream | 8x | 18 | ##### writable.writableHighWaterMark[#](#stream_writable_writablehighwatermark)
Added in: v8.10.0
Return the value of `highWaterMark` passed when constructing this
`Writable`.
##### writable.write(chunk[, encoding][, callback])[#](#stream_writable_write_chunk_encoding_callback)
History
| Version | Changes |
| --- ... | large_dataset/raw/docs/nodejs/stream/stream_8x.md |
99c9c25b-84c7-595b-a228-e354f38bf30c | docs | nodejs | nodejs_stream | 8x | 19 | * `encoding` [<string>](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type) The encoding, if `chunk` is a string
* `callback` [<Function>](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function) Callback for when this chunk of data is flushed
* Returns... | large_dataset/raw/docs/nodejs/stream/stream_8x.md |
93d82bba-a7a5-57a0-8d89-09a08d730efb | docs | nodejs | nodejs_stream | 8x | 20 | `highWaterMark` configured when the stream was created after admitting `chunk`.
If `false` is returned, further attempts to write data to the stream should
stop until the [`'drain'`](#stream_event_drain) event is emitted.
While a stream is not draining, calls to `write()` will buffer `chunk`, and
return false. Once al... | large_dataset/raw/docs/nodejs/stream/stream_8x.md |
b2758e48-f373-561c-9bb3-b97f2b7b4cd9 | docs | nodejs | nodejs_stream | 8x | 21 | drain if the remote peer does not read the data, writing a socket that is
not draining may lead to a remotely exploitable vulnerability.
Writing data while the stream is not draining is particularly
problematic for a [Transform](#stream_class_stream_transform), because the `Transform` streams are paused
by default unt... | large_dataset/raw/docs/nodejs/stream/stream_8x.md |
a9ca9942-8e1c-5594-86de-aaecf40f2e4c | docs | nodejs | nodejs_stream | 8x | 22 | console.log('write completed, do more writes now');
});
```
A Writable stream in object mode will always ignore the `encoding` argument.
##### writable.destroy([error])[#](#stream_writable_destroy_error)
Added in: v8.0.0
* Returns: [<this>](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operato... | large_dataset/raw/docs/nodejs/stream/stream_8x.md |
562e14c8-8043-5db5-a467-4b0b0fc54fdc | docs | nodejs | nodejs_stream | 8x | 23 | * [TCP sockets](net.html#net_class_net_socket)
* [child process stdout and stderr](child_process.html#child_process_subprocess_stdout)
* [`process.stdin`](process.html#process_process_stdin)
All [Readable](#stream_class_stream_readable) streams implement the interface defined by the
`stream.Readable` class.
#### Two ... | large_dataset/raw/docs/nodejs/stream/stream_8x.md |
34afce48-5133-5aec-a707-dd06bf8a2d7f | docs | nodejs | nodejs_stream | 8x | 24 | * Calling the [`stream.resume()`](#stream_readable_resume) method.
* Calling the [`stream.pipe()`](#stream_readable_pipe_destination_options) method to send the data to a [Writable](#stream_class_stream_writable).
The Readable can switch back to paused mode using one of the following:
* If there are no pipe destinati... | large_dataset/raw/docs/nodejs/stream/stream_8x.md |
cb76fc92-7c9c-5b86-ad7b-be06d9b62502 | docs | nodejs | nodejs_stream | 8x | 25 | destinations, then calling [`stream.pause()`](#stream_readable_pause) will not guarantee
that the stream will *remain* paused once those destinations drain and ask for
more data.
*Note*: If a [Readable](#stream_class_stream_readable) is switched into flowing mode and there are no
consumers available to handle the data... | large_dataset/raw/docs/nodejs/stream/stream_8x.md |
a3c43b08-0f70-55a2-992a-9f8c72498623 | docs | nodejs | nodejs_stream | 8x | 26 | When `readable._readableState.flowing` is `null`, no mechanism for consuming the
streams data is provided so the stream will not generate its data. While in this
state, attaching a listener for the `'data'` event, calling the `readable.pipe()`
method, or calling the `readable.resume()` method will switch
`readable._rea... | large_dataset/raw/docs/nodejs/stream/stream_8x.md |
0cbe9b55-933c-587a-a7fa-b0250f6d448a | docs | nodejs | nodejs_stream | 8x | 27 | pass.on('data', (chunk) => { console.log(chunk.toString()); });
pass.write('ok'); // will not emit 'data'
pass.resume(); // must be called to make 'data' being emitted
```
While `readable._readableState.flowing` is `false`, data may be accumulating
within the streams internal buffer.
#### Choose One[#](#stream_choose... | large_dataset/raw/docs/nodejs/stream/stream_8x.md |
f4dee055-9e34-507b-98a4-a08e6182f133 | docs | nodejs | nodejs_stream | 8x | 28 | #### Class: stream.Readable[#](#stream_class_stream_readable)
Added in: v0.9.4
##### Event: 'close'[#](#stream_event_close_1)
Added in: v0.9.4
The `'close'` event is emitted when the stream and any of its underlying
resources (a file descriptor, for example) have been closed. The event indicates
that no more events... | large_dataset/raw/docs/nodejs/stream/stream_8x.md |
48d2797a-a8a2-5de4-b270-d821d2dc5c06 | docs | nodejs | nodejs_stream | 8x | 29 | a chunk of data to a consumer. This may occur whenever the stream is switched
in flowing mode by calling `readable.pipe()`, `readable.resume()`, or by
attaching a listener callback to the `'data'` event. The `'data'` event will
also be emitted whenever the `readable.read()` method is called and a chunk of
data is avail... | large_dataset/raw/docs/nodejs/stream/stream_8x.md |
e1da5afc-967a-59ca-8541-948143ce536d | docs | nodejs | nodejs_stream | 8x | 30 | ##### Event: 'end'[#](#stream_event_end)
Added in: v0.9.4
The `'end'` event is emitted when there is no more data to be consumed from
the stream.
*Note*: The `'end'` event **will not be emitted** unless the data is
completely consumed. This can be accomplished by switching the stream into
flowing mode, or by calling... | large_dataset/raw/docs/nodejs/stream/stream_8x.md |
aef6460b-b625-56ab-9c7a-67bd5819f67b | docs | nodejs | nodejs_stream | 8x | 31 | due to an underlying internal failure, or when a stream implementation attempts
to push an invalid chunk of data.
The listener callback will be passed a single `Error` object.
##### Event: 'readable'[#](#stream_event_readable)
Added in: v0.9.4
The `'readable'` event is emitted when there is data available to be rea... | large_dataset/raw/docs/nodejs/stream/stream_8x.md |
9efa8a1b-9b7d-5661-9f7f-f9b35403bb0f | docs | nodejs | nodejs_stream | 8x | 32 | available data. In the latter case, [`stream.read()`](#stream_readable_read_size) will return
`null`. For instance, in the following example, `foo.txt` is an empty file:
```None
const fs = require('fs');
const rr = fs.createReadStream('foo.txt');
rr.on('readable', () => {
console.log(`readable: ${rr.read()}`);
});
r... | large_dataset/raw/docs/nodejs/stream/stream_8x.md |
28ac9dfc-537a-5d50-822c-f75f3ebe1718 | docs | nodejs | nodejs_stream | 8x | 33 | `readable.pipe()` method. In most typical cases, there will be no reason to
use this method directly.
```None
const readable = new stream.Readable();
readable.isPaused(); // === false
readable.pause();
readable.isPaused(); // === true
readable.resume();
readable.isPaused(); // === false
```
##### readable.pause()[#]... | large_dataset/raw/docs/nodejs/stream/stream_8x.md |
bc4e8ef7-5237-5163-8b7d-abe34754bfb5 | docs | nodejs | nodejs_stream | 8x | 34 | }, 1000);
});
```
##### readable.pipe(destination[, options])[#](#stream_readable_pipe_destination_options)
Added in: v0.9.4
* `destination` [<stream.Writable>](stream.html#stream_class_stream_writable) The destination for writing data
* `options` [<Object>](https://developer.mozilla.org/en-US/docs/Web/JavaScript/... | large_dataset/raw/docs/nodejs/stream/stream_8x.md |
a4e2d648-078b-574d-a33f-bf2f38c000af | docs | nodejs | nodejs_stream | 8x | 35 | const readable = getReadableStreamSomehow();
const writable = fs.createWriteStream('file.txt');
// All the data from readable goes into 'file.txt'
readable.pipe(writable);
```
It is possible to attach multiple Writable streams to a single Readable stream.
The `readable.pipe()` method returns a reference to the *desti... | large_dataset/raw/docs/nodejs/stream/stream_8x.md |
7d306ebb-edf6-5d95-937a-6c8ab966788f | docs | nodejs | nodejs_stream | 8x | 36 | writer.end('Goodbye\n');
});
```
One important caveat is that if the Readable stream emits an error during
processing, the Writable destination *is not closed* automatically. If an
error occurs, it will be necessary to *manually* close each stream in order
to prevent memory leaks.
*Note*: The [`process.stderr`](pro... | large_dataset/raw/docs/nodejs/stream/stream_8x.md |
ef8c2d23-d322-545d-af7c-fd909e99240a | docs | nodejs | nodejs_stream | 8x | 37 | * Returns: [<string>](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type) | [<Buffer>](buffer.html#buffer_class_buffer) | [<null>](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Null_type)
The `readable.read()` method pulls some data out of the internal buffer ... | large_dataset/raw/docs/nodejs/stream/stream_8x.md |
2d089636-ecc8-5763-b806-e0f403dd236c | docs | nodejs | nodejs_stream | 8x | 38 | in paused mode. In flowing mode, `readable.read()` is called automatically until
the internal buffer is fully drained.
```None
const readable = getReadableStreamSomehow();
readable.on('readable', () => {
let chunk;
while (null !== (chunk = readable.read())) {
console.log(`Received ${chunk.length} bytes of data... | large_dataset/raw/docs/nodejs/stream/stream_8x.md |
42f72e0b-39b7-5940-a7a5-d800be353671 | docs | nodejs | nodejs_stream | 8x | 39 | ##### readable.resume()[#](#stream_readable_resume)
Added in: v0.9.4
* Returns: [<this>](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/this)
The `readable.resume()` method causes an explicitly paused Readable stream to
resume emitting [`'data'`](#stream_event_data) events, switching the... | large_dataset/raw/docs/nodejs/stream/stream_8x.md |
c9cfb428-1abc-5845-b556-9924c5cca293 | docs | nodejs | nodejs_stream | 8x | 40 | The `readable.setEncoding()` method sets the character encoding for
data read from the Readable stream.
By default, no encoding is assigned and stream data will be returned as
`Buffer` objects. Setting an encoding causes the stream data
to be returned as strings of the specified encoding rather than as `Buffer`
object... | large_dataset/raw/docs/nodejs/stream/stream_8x.md |
0b6d3442-297b-5754-b461-eca4a4a928d3 | docs | nodejs | nodejs_stream | 8x | 41 | ##### readable.unpipe([destination])[#](#stream_readable_unpipe_destination)
Added in: v0.9.4
* `destination` [<stream.Writable>](stream.html#stream_class_stream_writable) Optional specific stream to unpipe
The `readable.unpipe()` method detaches a Writable stream previously attached
using the [`stream.pipe()`](#str... | large_dataset/raw/docs/nodejs/stream/stream_8x.md |
4d22c9fb-9f8e-5b21-b179-c50b4b58aa2a | docs | nodejs | nodejs_stream | 8x | 42 | ##### readable.unshift(chunk)[#](#stream_readable_unshift_chunk)
History
| Version | Changes |
| --- | --- |
| v8.0.0 | The `chunk` argument can now be a `Uint8Array` instance. |
| v0.9.11 | Added in: v0.9.11 |
* `chunk` [<Buffer>](buffer.html#buffer_class_buffer) | [<Uint8Array>](https://developer.mozilla.org/en-US... | large_dataset/raw/docs/nodejs/stream/stream_8x.md |
2e5a241f-9744-5620-963e-5b523bf3dbac | docs | nodejs | nodejs_stream | 8x | 43 | pulled out of the source, so that the data can be passed on to some other party.
*Note*: The `stream.unshift(chunk)` method cannot be called after the
[`'end'`](#stream_event_end) event has been emitted or a runtime error will be thrown.
Developers using `stream.unshift()` often should consider switching to
use of a ... | large_dataset/raw/docs/nodejs/stream/stream_8x.md |
7f377c8e-8b56-55e8-aadd-196e55a48846 | docs | nodejs | nodejs_stream | 8x | 44 | // found the header boundary
const split = str.split(/\n\n/);
header += split.shift();
const remaining = split.join('\n\n');
const buf = Buffer.from(remaining, 'utf8');
stream.removeListener('error', callback);
// remove the readable listener before unshifting
... | large_dataset/raw/docs/nodejs/stream/stream_8x.md |
cfdf52b5-568e-5559-a83a-2edc1c166e32 | docs | nodejs | nodejs_stream | 8x | 45 | implementation on a custom stream). Following the call to `readable.unshift()`
with an immediate [`stream.push('')`](#stream_readable_push_chunk_encoding) will reset the reading state
appropriately, however it is best to simply avoid calling `readable.unshift()`
while in the process of performing a read.
##### readabl... | large_dataset/raw/docs/nodejs/stream/stream_8x.md |
ee733ea2-e5a9-51ce-8d79-4a3e4c6832c0 | docs | nodejs | nodejs_stream | 8x | 46 | It will rarely be necessary to use `readable.wrap()` but the method has been
provided as a convenience for interacting with older Node.js applications and
libraries.
For example:
```None
const { OldReader } = require('./old-api-module.js');
const { Readable } = require('stream');
const oreader = new OldReader();
cons... | large_dataset/raw/docs/nodejs/stream/stream_8x.md |
912913c4-6c25-5956-b3c4-7a8fe36f3453 | docs | nodejs | nodejs_stream | 8x | 47 | #### Class: stream.Duplex[#](#stream_class_stream_duplex)
History
| Version | Changes |
| --- | --- |
| v6.8.0 | Instances of `Duplex` now return `true` when checking `instanceof stream.Writable`. |
| v0.9.4 | Added in: v0.9.4 |
Duplex streams are streams that implement both the [Readable](#stream_class_stream_reada... | large_dataset/raw/docs/nodejs/stream/stream_8x.md |
78c20d09-b2a5-59ad-b2ee-7413832ebe3e | docs | nodejs | nodejs_stream | 8x | 48 | * [crypto streams](crypto.html)
##### transform.destroy([error])[#](#stream_transform_destroy_error)
Added in: v8.0.0
Destroy the stream, and emit `'error'`. After this call, the
transform stream would release any internal resources.
implementors should not override this method, but instead implement
[`readable._des... | large_dataset/raw/docs/nodejs/stream/stream_8x.md |
5d9d4fe1-ea8a-539e-a903-0045136db388 | docs | nodejs | nodejs_stream | 8x | 49 | constructor(options) {
super(options);
// ...
}
}
```
The new stream class must then implement one or more specific methods, depending
on the type of stream being created, as detailed in the chart below:
| Use-case | Class | Method(s) to implement |
| --- | --- | --- |
| Reading only | [Readable](#stream_... | large_dataset/raw/docs/nodejs/stream/stream_8x.md |
fe7ea008-e746-5616-b001-127e576647c9 | docs | nodejs | nodejs_stream | 8x | 50 | ### Simplified Construction[#](#stream_simplified_construction)
Added in: v1.2.0
For many simple cases, it is possible to construct a stream without relying on
inheritance. This can be accomplished by directly creating instances of the
`stream.Writable`, `stream.Readable`, `stream.Duplex` or `stream.Transform`
object... | large_dataset/raw/docs/nodejs/stream/stream_8x.md |
94d7711e-9eac-570c-90c1-fcf53f95f36e | docs | nodejs | nodejs_stream | 8x | 51 | #### Constructor: new stream.Writable([options])[#](#stream_constructor_new_stream_writable_options)
* `options` [<Object>](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object)
+ `highWaterMark` [<number>](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Num... | large_dataset/raw/docs/nodejs/stream/stream_8x.md |
5e34ecc5-4a5a-564f-a95a-beaf23832136 | docs | nodejs | nodejs_stream | 8x | 52 | [`stream.write(anyObj)`](#stream_writable_write_chunk_encoding_callback) is a valid operation. When set,
it becomes possible to write JavaScript values other than string,
`Buffer` or `Uint8Array` if supported by the stream implementation.
**Default:** `false`.
+ `write` [<Function>](https://developer.... | large_dataset/raw/docs/nodejs/stream/stream_8x.md |
65211145-fd50-5894-9753-3848550ababe | docs | nodejs | nodejs_stream | 8x | 53 | + `final` [<Function>](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function) Implementation for the
[`stream._final()`](#stream_writable_final_callback) method.
For example:
```None
const { Writable } = require('stream');
class MyWritable extends Writable {
constructor(op... | large_dataset/raw/docs/nodejs/stream/stream_8x.md |
41a8a9fa-97e7-51a5-baa6-c891e0f48de3 | docs | nodejs | nodejs_stream | 8x | 54 | #### writable.\_write(chunk, encoding, callback)[#](#stream_writable_write_chunk_encoding_callback_1)
* `chunk` [<Buffer>](buffer.html#buffer_class_buffer) | [<string>](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type) | <any> The chunk to be written. Will **always**
be a buffer un... | large_dataset/raw/docs/nodejs/stream/stream_8x.md |
4b51ed65-c9e0-5108-b0eb-67a14b8839fb | docs | nodejs | nodejs_stream | 8x | 55 | [`writable._write()`](#stream_writable_write_chunk_encoding_callback_1) method to send data to the underlying
resource.
*Note*: [Transform](#stream_class_stream_transform) streams provide their own implementation of the
[`writable._write()`](#stream_writable_write_chunk_encoding_callback_1).
*Note*: This function MUS... | large_dataset/raw/docs/nodejs/stream/stream_8x.md |
87520d7a-79c1-54fe-a91f-5738e5ec715f | docs | nodejs | nodejs_stream | 8x | 56 | event. If a stream implementation is capable of processing multiple chunks of
data at once, the `writable._writev()` method should be implemented.
If the `decodeStrings` property is set in the constructor options, then
`chunk` may be a string rather than a Buffer, and `encoding` will
indicate the character encoding of... | large_dataset/raw/docs/nodejs/stream/stream_8x.md |
93415231-d22d-526b-ae05-d9b4004a43b5 | docs | nodejs | nodejs_stream | 8x | 57 | * `chunks` [<Array>](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array) The chunks to be written. Each chunk has following
format: `{ chunk: ..., encoding: ... }`.
* `callback` [<Function>](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function) ... | large_dataset/raw/docs/nodejs/stream/stream_8x.md |
9522c240-1fc8-5db7-b8d2-17c436f93666 | docs | nodejs | nodejs_stream | 8x | 58 | internal to the class that defines it, and should never be called directly by
user programs.
#### writable.\_destroy(err, callback)[#](#stream_writable_destroy_err_callback)
Added in: v8.0.0
* `err` [<Error>](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error) A possible error.
* ... | large_dataset/raw/docs/nodejs/stream/stream_8x.md |
06e72cd4-bf05-5788-abe7-f7b7a76dfae4 | docs | nodejs | nodejs_stream | 8x | 59 | The `_final()` method **must not** be called directly. It may be implemented
by child classes, and if so, will be called by the internal Writable
class methods only.
This optional function will be called before the stream closes, delaying the
`finish` event until `callback` is called. This is useful to close resources... | large_dataset/raw/docs/nodejs/stream/stream_8x.md |
935f2ad2-954b-56d4-bfae-779789658928 | docs | nodejs | nodejs_stream | 8x | 60 | error, the Readable stream will be unpiped.
```None
const { Writable } = require('stream');
const myWritable = new Writable({
write(chunk, encoding, callback) {
if (chunk.toString().indexOf('a') >= 0) {
callback(new Error('chunk is invalid'));
} else {
callback();
}
}
});
```
#### An Exam... | large_dataset/raw/docs/nodejs/stream/stream_8x.md |
fbe46ffd-5047-576a-964d-4c035c4c0a4a | docs | nodejs | nodejs_stream | 8x | 61 | ```
#### Decoding buffers in a Writable Stream[#](#stream_decoding_buffers_in_a_writable_stream)
Decoding buffers is a common task, for instance, when using transformers whose
input is a string. This is not a trivial process when using multi-byte
characters encoding, such as UTF-8. The following example shows how to ... | large_dataset/raw/docs/nodejs/stream/stream_8x.md |
b44694a5-ab8c-5ba4-a4db-39de3709b779 | docs | nodejs | nodejs_stream | 8x | 62 | const euro = [[0xE2, 0x82], [0xAC]].map(Buffer.from);
const w = new StringWritable();
w.write('currency: ');
w.write(euro[0]);
w.end(euro[1]);
console.log(w.data); // currency: €
```
### Implementing a Readable Stream[#](#stream_implementing_a_readable_stream)
The `stream.Readable` class is extended to implement a ... | large_dataset/raw/docs/nodejs/stream/stream_8x.md |
e4a8a1f9-452a-5eed-8421-cf4bb7265ce5 | docs | nodejs | nodejs_stream | 8x | 63 | **Default:** `16384` (16kb), or `16` for `objectMode` streams.
+ `encoding` [<string>](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type) If specified, then buffers will be decoded to
strings using the specified encoding. **Default:** `null`.
+ `objectMode` [<boolean>](http... | large_dataset/raw/docs/nodejs/stream/stream_8x.md |
4ac3c897-9e99-5326-9062-1e613f2defc1 | docs | nodejs | nodejs_stream | 8x | 64 | For example:
```None
const { Readable } = require('stream');
class MyReadable extends Readable {
constructor(options) {
// Calls the stream.Readable(options) constructor
super(options);
// ...
}
}
```
Or, when using pre-ES6 style constructors:
```None
const { Readable } = require('stream');
const ut... | large_dataset/raw/docs/nodejs/stream/stream_8x.md |
003f856c-d54a-505c-b410-bc42a68121cc | docs | nodejs | nodejs_stream | 8x | 65 | should be implemented by child classes, and called by the internal Readable
class methods only.
All Readable stream implementations must provide an implementation of the
`readable._read()` method to fetch data from the underlying resource.
When `readable._read()` is called, if data is available from the resource, the... | large_dataset/raw/docs/nodejs/stream/stream_8x.md |
79740acf-c129-51f2-978e-39a547914844 | docs | nodejs | nodejs_stream | 8x | 66 | much data to fetch. Other implementations may ignore this argument and simply
provide data whenever it becomes available. There is no need to "wait" until
`size` bytes are available before calling [`stream.push(chunk)`](#stream_readable_push_chunk_encoding).
The `readable._read()` method is prefixed with an underscore... | large_dataset/raw/docs/nodejs/stream/stream_8x.md |
ea89f5fe-f631-5588-a2c1-88b296b3be68 | docs | nodejs | nodejs_stream | 8x | 67 | #### readable.push(chunk[, encoding])[#](#stream_readable_push_chunk_encoding)
History
| Version | Changes |
| --- | --- |
| v8.0.0 | The `chunk` argument can now be a `Uint8Array` instance. |
* `chunk` [<Buffer>](buffer.html#buffer_class_buffer) | [<Uint8Array>](https://developer.mozilla.org/en-US/docs/Web/JavaScri... | large_dataset/raw/docs/nodejs/stream/stream_8x.md |
0afc0bf9-53cc-5036-a959-d112a22615e2 | docs | nodejs | nodejs_stream | 8x | 68 | * Returns: [<boolean>](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type) `true` if additional chunks of data may continued to be
pushed; `false` otherwise.
When `chunk` is a `Buffer`, `Uint8Array` or `string`, the `chunk` of data will
be added to the internal queue for users of th... | large_dataset/raw/docs/nodejs/stream/stream_8x.md |
c061bcfa-47c6-51d2-b626-3b6bdd30521a | docs | nodejs | nodejs_stream | 8x | 69 | pause/resume mechanism, and a data callback, the low-level source can be wrapped
by the custom Readable instance as illustrated in the following example:
```None
// 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 ge... | large_dataset/raw/docs/nodejs/stream/stream_8x.md |
913fcf9c-59b1-5787-9369-4049bfef9042 | docs | nodejs | nodejs_stream | 8x | 70 | this._source.readStart();
}
}
```
*Note*: The `readable.push()` method is intended be called only by Readable
Implementers, and only from within the `readable._read()` method.
For streams not operating in object mode, if the `chunk` parameter of
`readable.push()` is `undefined`, it will be treated as empty stri... | large_dataset/raw/docs/nodejs/stream/stream_8x.md |
14355fb2-6565-5b58-b06d-15121c50345a | docs | nodejs | nodejs_stream | 8x | 71 | process.nextTick(() => this.emit('error', err));
return;
}
// do some work
}
});
```
#### An Example Counting Stream[#](#stream_an_example_counting_stream)
The following is a basic example of a Readable stream that emits the numerals
from 1 to 1,000,000 in ascending order, and then ends.
```Non... | large_dataset/raw/docs/nodejs/stream/stream_8x.md |
4d3f4298-08b6-5787-bd0b-e556ce79e41e | docs | nodejs | nodejs_stream | 8x | 72 | Because JavaScript does not have support for multiple inheritance, the
`stream.Duplex` class is extended to implement a [Duplex](#stream_class_stream_duplex) stream (as opposed
to extending the `stream.Readable` *and* `stream.Writable` classes).
*Note*: The `stream.Duplex` class prototypically inherits from
`stream.Re... | large_dataset/raw/docs/nodejs/stream/stream_8x.md |
a2c96816-7e41-5086-bcbb-90f0e6a26de9 | docs | nodejs | nodejs_stream | 8x | 73 | * `options` [<Object>](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object) Passed to both Writable and Readable
constructors. Also has the following fields:
+ `allowHalfOpen` [<boolean>](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type) If set... | large_dataset/raw/docs/nodejs/stream/stream_8x.md |
963cb770-a4f5-579d-a651-5c652c5753e6 | docs | nodejs | nodejs_stream | 8x | 74 | + `readableHighWaterMark` [<number>](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type) Sets `highWaterMark` for the readable side
of the stream. Has no effect if `highWaterMark` is provided.
+ `writableHighWaterMark` [<number>](https://developer.mozilla.org/en-US/docs/Web/Java... | large_dataset/raw/docs/nodejs/stream/stream_8x.md |
eec455b1-5ac2-5a22-b6f4-fcb1c34e36e3 | docs | nodejs | nodejs_stream | 8x | 75 | const myDuplex = new Duplex({
read(size) {
// ...
},
write(chunk, encoding, callback) {
// ...
}
});
```
#### An Example Duplex Stream[#](#stream_an_example_duplex_stream)
The following illustrates a simple example of a Duplex stream that wraps a
hypothetical lower-level source object to which data ca... | large_dataset/raw/docs/nodejs/stream/stream_8x.md |
0bca6f56-28f2-546b-b433-ccb27957dbda | docs | nodejs | nodejs_stream | 8x | 76 | chunk = chunk.toString();
this[kSource].writeSomeData(chunk);
callback();
}
_read(size) {
this[kSource].fetchSomeData(size, (data, encoding) => {
this.push(Buffer.from(data, encoding));
});
}
}
```
The most important aspect of a Duplex stream is that the Readable and Writable
sides o... | large_dataset/raw/docs/nodejs/stream/stream_8x.md |
68ca3347-bb51-560d-ad35-4fa5c0ba05f4 | docs | nodejs | nodejs_stream | 8x | 77 | // All Transform streams are also Duplex Streams
const myTransform = new Transform({
writableObjectMode: true,
transform(chunk, encoding, callback) {
// Coerce the chunk to a number if necessary
chunk |= 0;
// Transform the chunk into something else.
const data = chunk.toString(16);
// Push t... | large_dataset/raw/docs/nodejs/stream/stream_8x.md |
dbb86431-39fc-59fc-b54b-5e89e438a6cc | docs | nodejs | nodejs_stream | 8x | 78 | *Note*: There is no requirement that the output be the same size as the input,
the same number of chunks, or arrive at the same time. For example, a
Hash stream will only ever have a single chunk of output which is
provided when the input is ended. A `zlib` stream will produce output
that is either much smaller or much... | large_dataset/raw/docs/nodejs/stream/stream_8x.md |
84995b88-d7a8-5a79-8a04-a09d1e2d04b9 | docs | nodejs | nodejs_stream | 8x | 79 | the output on the Readable side is not consumed.
#### new stream.Transform([options])[#](#stream_new_stream_transform_options)
* `options` [<Object>](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object) Passed to both Writable and Readable
constructors. Also has the following fie... | large_dataset/raw/docs/nodejs/stream/stream_8x.md |
69f201e8-eec1-581e-bcc9-bf75111d3a4c | docs | nodejs | nodejs_stream | 8x | 80 | const util = require('util');
function MyTransform(options) {
if (!(this instanceof MyTransform))
return new MyTransform(options);
Transform.call(this, options);
}
util.inherits(MyTransform, Transform);
```
Or, using the Simplified Constructor approach:
```None
const { Transform } = require('stream');
const... | large_dataset/raw/docs/nodejs/stream/stream_8x.md |
c2ea1f2c-775c-587c-8cf9-e97a2099f726 | docs | nodejs | nodejs_stream | 8x | 81 | [`transform._flush()`](#stream_transform_flush_callback) has been called.
#### transform.\_flush(callback)[#](#stream_transform_flush_callback)
* `callback` [<Function>](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function) A callback function (optionally with an error
argument ... | large_dataset/raw/docs/nodejs/stream/stream_8x.md |
da8c6d6e-d33f-560e-8265-b51c93addf22 | docs | nodejs | nodejs_stream | 8x | 82 | method. This will be called when there is no more written data to be consumed,
but before the [`'end'`](#stream_event_end) event is emitted signaling the end of the
[Readable](#stream_class_stream_readable) stream.
Within the `transform._flush()` implementation, the `readable.push()` method
may be called zero or more ... | large_dataset/raw/docs/nodejs/stream/stream_8x.md |
5eb0f83b-9a54-51e3-aa55-33a3d8830be3 | docs | nodejs | nodejs_stream | 8x | 83 | * `encoding` [<string>](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type) If the chunk is a string, then this is the
encoding type. If chunk is a buffer, then this is the special
value - 'buffer', ignore it in this case.
* `callback` [<Function>](https://developer.mozilla.org/en-U... | large_dataset/raw/docs/nodejs/stream/stream_8x.md |
216dc671-4e37-5b2b-a45a-b7f6007698bc | docs | nodejs | nodejs_stream | 8x | 84 | The `transform.push()` method may be called zero or more times to generate
output from a single input chunk, depending on how much is to be output
as a result of the chunk.
It is possible that no output is generated from any given chunk of input data.
The `callback` function must be called only when the current chunk... | large_dataset/raw/docs/nodejs/stream/stream_8x.md |
884f203e-a97e-5227-8ea9-f0e01e528db0 | docs | nodejs | nodejs_stream | 8x | 85 | user programs.
`transform._transform()` is never called in parallel; streams implement a
queue mechanism, and to receive the next chunk, `callback` must be
called, either synchronously or asynchronously.
#### Class: stream.PassThrough[#](#stream_class_stream_passthrough)
The `stream.PassThrough` class is a trivial i... | large_dataset/raw/docs/nodejs/stream/stream_8x.md |
e22860d9-e492-526b-af0e-17d92239c164 | docs | nodejs | nodejs_stream | 8x | 86 | [`'data'`](#stream_event_data) events would begin emitting immediately. Applications that
would need to perform some amount of work to decide how to handle data
were required to store read data into buffers so the data would not be lost.
* The [`stream.pause()`](#stream_readable_pause) method was advisory, rather... | large_dataset/raw/docs/nodejs/stream/stream_8x.md |
6b768f77-b774-5818-8ec3-4211b6ece3b1 | docs | nodejs | nodejs_stream | 8x | 87 | [`'data'`](#stream_event_data) chunks.
While most applications will continue to function normally, this introduces an
edge case in the following conditions:
* No [`'data'`](#stream_event_data) event listener is added.
* The [`stream.resume()`](#stream_readable_resume) method is never called.
* The stream is not piped... | large_dataset/raw/docs/nodejs/stream/stream_8x.md |
1d58ad95-abca-524a-817c-f3b555d5ab8d | docs | nodejs | nodejs_stream | 8x | 88 | socket.on('end', () => {
socket.end('The message was received but was not processed.\n');
});
// start the flow of data, discarding it.
socket.resume();
}).listen(1337);
```
In addition to new Readable streams switching into flowing mode,
pre-v0.10 style streams can be wrapped in a Readable class using t... | large_dataset/raw/docs/nodejs/stream/stream_8x.md |
e88e8ae6-8319-54c3-952e-1bb3b19f13ce | docs | nodejs | nodejs_stream | 8x | 89 | Readable stream class internals.
### `readable.push('')`[#](#stream_readable_push)
Use of `readable.push('')` is not recommended.
Pushing a zero-byte string, `Buffer` or `Uint8Array` to a stream that is not in
object mode has an interesting side effect. Because it *is* a call to
[`readable.push()`](#stream_readable_... | large_dataset/raw/docs/nodejs/stream/stream_8x.md |
c3f0afa0-24e4-58d6-bb54-f78d60fa5447 | docs | nodejs | nodejs_stream | 8x | 90 | This is not a problem in common cases with `latin1` or `ascii`. But it is
advised to be mindful about this behavior when working with strings that could
contain multi-byte characters.
| large_dataset/raw/docs/nodejs/stream/stream_8x.md |
dded6025-0a8f-57ca-bc0a-b3198d88193f | docs | nodejs | nodejs_stream | 9x | 0 | # Stream#
Source: https://nodejs.org/docs/latest-v9.x/api/stream.html
Version: 9.x
[Stability: 2](documentation.html#documentation_stability_index) - Stable
A stream is an abstract interface for working with streaming data in Node.js.
The `stream` module provides a base API that makes it easy to build objects
that i... | large_dataset/raw/docs/nodejs/stream/stream_9x.md |
7efc9537-c115-5d9e-a6ad-f0638ec098af | docs | nodejs | nodejs_stream | 9x | 1 | Developers who are primarily *consuming* stream objects will rarely need to use
the `stream` module directly.
## Organization of this Document[#](#stream_organization_of_this_document)
This document is divided into two primary sections with a third section for
additional notes. The first section explains the elements... | large_dataset/raw/docs/nodejs/stream/stream_9x.md |
33bab21a-d0fc-58c3-825b-5b1fddaf70a1 | docs | nodejs | nodejs_stream | 9x | 2 | * [Duplex](#stream_class_stream_duplex) - streams that are both Readable and Writable (for example
[`net.Socket`](net.html#net_class_net_socket)).
* [Transform](#stream_class_stream_transform) - Duplex streams that can modify or transform the data as it
is written and read (for example [`zlib.createDeflate()`](zlib... | large_dataset/raw/docs/nodejs/stream/stream_9x.md |
b8aa0400-f13c-5528-a50d-c58d646c3328 | docs | nodejs | nodejs_stream | 9x | 3 | ### Buffering[#](#stream_buffering)
Both [Writable](#stream_class_stream_writable) and [Readable](#stream_class_stream_readable) streams will store data in an internal
buffer that can be retrieved using `writable.writableBuffer` or
`readable.readableBuffer`, respectively.
The amount of data potentially buffered depen... | large_dataset/raw/docs/nodejs/stream/stream_9x.md |
dbaa4ec3-a755-5405-80ca-fbd36ad5bdec | docs | nodejs | nodejs_stream | 9x | 4 | by `highWaterMark`, the stream will temporarily stop reading data from the
underlying resource until the data currently buffered can be consumed (that is,
the stream will stop calling the internal `readable._read()` method that is
used to fill the read buffer).
Data is buffered in Writable streams when the
[`writable.... | large_dataset/raw/docs/nodejs/stream/stream_9x.md |
7f91fc3b-6089-5849-a9b5-83848c52ea87 | docs | nodejs | nodejs_stream | 9x | 5 | Because [Duplex](#stream_class_stream_duplex) and [Transform](#stream_class_stream_transform) streams are both Readable and Writable,
each maintain *two* separate internal buffers used for reading and writing,
allowing each side to operate independently of the other while maintaining an
appropriate and efficient flow o... | large_dataset/raw/docs/nodejs/stream/stream_9x.md |
97eb52c3-1bbd-53fb-8606-7d083354216d | docs | nodejs | nodejs_stream | 9x | 6 | const http = require('http');
const server = http.createServer((req, res) => {
// req is an http.IncomingMessage, which is a Readable Stream
// res is an http.ServerResponse, which is a Writable Stream
let body = '';
// Get the data as utf8 strings.
// If an encoding is not set, Buffer objects will be recei... | large_dataset/raw/docs/nodejs/stream/stream_9x.md |
b75dad96-feb8-5926-b1be-e9929ffc95c6 | docs | nodejs | nodejs_stream | 9x | 7 | // error: Unexpected token o in JSON at position 1
```
[Writable](#stream_class_stream_writable) streams (such as `res` in the example) expose methods such as
`write()` and `end()` that are used to write data onto the stream.
[Readable](#stream_class_stream_readable) streams use the [`EventEmitter`](events.html#event... | large_dataset/raw/docs/nodejs/stream/stream_9x.md |
443995c9-685f-5c11-b241-752e2b6d0bd3 | docs | nodejs | nodejs_stream | 9x | 8 | are not required to implement the stream interfaces directly and will generally
have no reason to call `require('stream')`.
Developers wishing to implement new types of streams should refer to the
section [API for Stream Implementers](#stream_api_for_stream_implementers).
### Writable Streams[#](#stream_writable_stre... | large_dataset/raw/docs/nodejs/stream/stream_9x.md |
f7033f7b-4cd8-5573-a63e-e1b2db3ac227 | docs | nodejs | nodejs_stream | 9x | 9 | Some of these examples are actually [Duplex](#stream_class_stream_duplex) streams that implement the
[Writable](#stream_class_stream_writable) interface.
All [Writable](#stream_class_stream_writable) streams implement the interface defined by the
`stream.Writable` class.
While specific instances of [Writable](#stream... | large_dataset/raw/docs/nodejs/stream/stream_9x.md |
b26ccf12-a102-555c-8981-e26944027e97 | docs | nodejs | nodejs_stream | 9x | 10 | Not all Writable streams will emit the `'close'` event.
##### Event: 'drain'[#](#stream_event_drain)
Added in: v0.9.4
If a call to [`stream.write(chunk)`](#stream_writable_write_chunk_encoding_callback) returns `false`, the
`'drain'` event will be emitted when it is appropriate to resume writing data
to the stream.
... | large_dataset/raw/docs/nodejs/stream/stream_9x.md |
8cf8399e-3ec9-552d-9a87-e0cc9e1d456f | docs | nodejs | nodejs_stream | 9x | 11 | ##### Event: 'error'[#](#stream_event_error)
Added in: v0.9.4
* [<Error>](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error)
The `'error'` event is emitted if an error occurred while writing or piping
data. The listener callback is passed a single `Error` argument when called.
T... | large_dataset/raw/docs/nodejs/stream/stream_9x.md |
5aed0753-cd46-5cab-aa23-3363b0691d53 | docs | nodejs | nodejs_stream | 9x | 12 | ##### Event: 'pipe'[#](#stream_event_pipe)
Added in: v0.9.4
* `src` [<stream.Readable>](stream.html#stream_class_stream_readable) source stream that is piping to this writable
The `'pipe'` event is emitted when the [`stream.pipe()`](#stream_readable_pipe_destination_options) method is called on
a readable stream, ad... | large_dataset/raw/docs/nodejs/stream/stream_9x.md |
bae830b6-df59-542f-af01-ba55ee643322 | docs | nodejs | nodejs_stream | 9x | 13 | on a [Readable](#stream_class_stream_readable) stream, removing this [Writable](#stream_class_stream_writable) from its set of
destinations.
This is also emitted in case this [Writable](#stream_class_stream_writable) stream emits an error when a
[Readable](#stream_class_stream_readable) stream pipes into it.
```None
... | large_dataset/raw/docs/nodejs/stream/stream_9x.md |
b77fbc60-f5e9-574b-acb8-696286c77e42 | docs | nodejs | nodejs_stream | 9x | 14 | many small chunks of data to a stream do not cause a backup in the internal
buffer that would have an adverse impact on performance. In such situations,
implementations that implement the `writable._writev()` method can perform
buffered writes in a more optimized manner.
See also: [`writable.uncork()`](#stream_writabl... | large_dataset/raw/docs/nodejs/stream/stream_9x.md |
8e04fa4e-1c2c-554d-ba9c-0c5f88cce4a3 | docs | nodejs | nodejs_stream | 9x | 15 | `Uint8Array`. For object mode streams, `chunk` may be any JavaScript value
other than `null`.
* `encoding` [<string>](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type) The encoding, if `chunk` is a string
* `callback` [<Function>](https://developer.mozilla.org/en-US/docs/Web/JavaS... | large_dataset/raw/docs/nodejs/stream/stream_9x.md |
379b9abc-d316-5c77-a2ec-efe7f538167a | docs | nodejs | nodejs_stream | 9x | 16 | // write 'hello, ' and then end with 'world!'
const fs = require('fs');
const file = fs.createWriteStream('example.txt');
file.write('hello, ');
file.end('world!');
// writing more now is not allowed!
```
##### writable.setDefaultEncoding(encoding)[#](#stream_writable_setdefaultencoding_encoding)
History
| Version |... | large_dataset/raw/docs/nodejs/stream/stream_9x.md |
ab5e4691-c3a3-532e-bcce-e997569e284c | docs | nodejs | nodejs_stream | 9x | 17 | ##### writable.uncork()[#](#stream_writable_uncork)
Added in: v0.11.2
The `writable.uncork()` method flushes all data buffered since
[`stream.cork()`](#stream_writable_cork) was called.
When using [`writable.cork()`](#stream_writable_cork) and `writable.uncork()` to manage the buffering
of writes to a stream, it is ... | large_dataset/raw/docs/nodejs/stream/stream_9x.md |
815b4426-c716-579a-87da-06217f8d1024 | docs | nodejs | nodejs_stream | 9x | 18 | stream.uncork();
});
```
See also: [`writable.cork()`](#stream_writable_cork).
##### writable.writableHighWaterMark[#](#stream_writable_writablehighwatermark)
Added in: v9.3.0
Return the value of `highWaterMark` passed when constructing this
`Writable`.
##### writable.writableLength[#](#stream_writable_writablel... | large_dataset/raw/docs/nodejs/stream/stream_9x.md |
3e82943f-c24a-5591-98b5-4d047d33632f | docs | nodejs | nodejs_stream | 9x | 19 | * `chunk` [<string>](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type) | [<Buffer>](buffer.html#buffer_class_buffer) | [<Uint8Array>](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Uint8Array) | <any> Optional data to write. For streams
not operatin... | large_dataset/raw/docs/nodejs/stream/stream_9x.md |
f858ecfe-ac8b-5267-a0ef-8d33fbefa112 | docs | nodejs | nodejs_stream | 9x | 20 | additional data; otherwise `true`.
The `writable.write()` method writes some data to the stream, and calls the
supplied `callback` once the data has been fully handled. If an error
occurs, the `callback` *may or may not* be called with the error as its
first argument. To reliably detect write errors, add a listener ... | large_dataset/raw/docs/nodejs/stream/stream_9x.md |
bd5f3328-2122-50aa-a5e4-e39b79c02108 | docs | nodejs | nodejs_stream | 9x | 21 | is not draining is allowed, Node.js will buffer all written chunks until
maximum memory usage occurs, at which point it will abort unconditionally.
Even before it aborts, high memory usage will cause poor garbage collector
performance and high RSS (which is not typically released back to the system,
even after the memo... | large_dataset/raw/docs/nodejs/stream/stream_9x.md |
55bc0ce5-7047-5a29-8e18-047d09ab50e6 | docs | nodejs | nodejs_stream | 9x | 22 | [`stream.pipe()`](#stream_readable_pipe_destination_options). However, if calling `write()` is preferred, it is
possible to respect backpressure and avoid memory issues using the
[`'drain'`](#stream_event_drain) event:
```None
function write(data, cb) {
if (!stream.write(data)) {
stream.once('drain', cb);
} el... | large_dataset/raw/docs/nodejs/stream/stream_9x.md |
f4cf31a1-2e15-59f2-908b-bd95c2e4443a | docs | nodejs | nodejs_stream | 9x | 23 | ### Readable Streams[#](#stream_readable_streams)
Readable streams are an abstraction for a *source* from which data is
consumed.
Examples of Readable streams include:
* [HTTP responses, on the client](http.html#http_class_http_incomingmessage)
* [HTTP requests, on the server](http.html#http_class_http_incomingmessa... | large_dataset/raw/docs/nodejs/stream/stream_9x.md |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.