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 |
|---|---|---|---|---|---|---|---|
54b13b67-a173-568e-a9d8-6ccf9f8ad58c | docs | nodejs | nodejs_stream | 4x | 15 | #### readable.setEncoding(encoding)[#](#stream_readable_setencoding_encoding)
* `encoding` [<String>](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type) The encoding to use.
* Returns: `this`
Call this function to cause the stream to return strings of the specified
encoding instead o... | large_dataset/raw/docs/nodejs/stream/stream_4x.md |
f7ff10f1-2f16-570d-aee4-79368291800c | docs | nodejs | nodejs_stream | 4x | 16 | multi-byte strings spread out over multiple chunks.
```None
var readable = getReadableStreamSomehow();
readable.setEncoding('utf8');
readable.on('data', (chunk) => {
assert.equal(typeof chunk, 'string');
console.log('got %d characters of string data', chunk.length);
});
```
#### readable.unpipe([destination])[#](... | large_dataset/raw/docs/nodejs/stream/stream_4x.md |
542a3c50-addf-536e-8466-9b03c888aef4 | docs | nodejs | nodejs_stream | 4x | 17 | readable.unpipe(writable);
console.log('manually close the file stream');
writable.end();
}, 1000);
```
#### readable.unshift(chunk)[#](#stream_readable_unshift_chunk)
* `chunk` [<Buffer>](buffer.html#buffer_class_buffer) | [<String>](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Strin... | large_dataset/raw/docs/nodejs/stream/stream_4x.md |
b4e88bfd-be4f-56e4-a883-3cba97a06ece | docs | nodejs | nodejs_stream | 4x | 18 | // Pull off a header delimited by \n\n
// use unshift() if we get too much
// Call the callback with (error, header, stream)
const StringDecoder = require('string_decoder').StringDecoder;
function parseHeader(stream, callback) {
stream.on('error', callback);
stream.on('readable', onReadable);
var decoder = new St... | large_dataset/raw/docs/nodejs/stream/stream_4x.md |
23b8d890-1720-5ceb-b9e9-c61e12c95102 | docs | nodejs | nodejs_stream | 4x | 19 | // still reading the header.
header += str;
}
}
}
}
```
Note that, unlike [`stream.push(chunk)`](#stream_readable_push_chunk_encoding), `stream.unshift(chunk)`
will not end the reading process by resetting the internal reading state of the
stream. This can cause unexpected results if `unshi... | large_dataset/raw/docs/nodejs/stream/stream_4x.md |
f7ed3863-462b-5ff6-85ff-5bcca0c10ab2 | docs | nodejs | nodejs_stream | 4x | 20 | entire Streams API as it is today. (See [Compatibility](#stream_compatibility_with_older_node_js_versions) for
more information.)
If you are using an older Node.js library that emits [`'data'`](#stream_event_data) events and
has a [`stream.pause()`](#stream_readable_pause) method that is advisory only, then you
can us... | large_dataset/raw/docs/nodejs/stream/stream_4x.md |
d783c5b3-c022-5c05-af5a-69b0b10f71d4 | docs | nodejs | nodejs_stream | 4x | 21 | ### Class: stream.Transform[#](#stream_class_stream_transform)
Transform streams are [Duplex](#stream_class_stream_duplex) streams where the output is in some way
computed from the input. They implement both the [Readable](#stream_class_stream_readable) and
[Writable](#stream_class_stream_writable) interfaces.
Exampl... | large_dataset/raw/docs/nodejs/stream/stream_4x.md |
49aedb6e-3a63-5367-94b6-c6c264698231 | docs | nodejs | nodejs_stream | 4x | 22 | * [`process.stdout`](process.html#process_process_stdout), [`process.stderr`](process.html#process_process_stderr)
#### Event: 'close'[#](#stream_event_close_1)
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 will b... | large_dataset/raw/docs/nodejs/stream/stream_4x.md |
64e8bfef-b2f7-56ad-bbf3-2e331cc94eaf | docs | nodejs | nodejs_stream | 4x | 23 | writer.write(data, encoding, callback);
} else {
// see if we should continue, or wait
// don't pass the callback, because we're not done yet.
ok = writer.write(data, encoding);
}
} while (i > 0 && ok);
if (i > 0) {
// had to stop early!
// write some more... | large_dataset/raw/docs/nodejs/stream/stream_4x.md |
c80f1c5c-5205-5758-a5d3-a31fdebff18e | docs | nodejs | nodejs_stream | 4x | 24 | console.error('all writes are now complete.');
});
```
#### Event: 'pipe'[#](#stream_event_pipe)
* `src` [<stream.Readable>](stream.html#stream_class_stream_readable) source stream that is piping to this writable
This is emitted whenever the [`stream.pipe()`](#stream_readable_pipe_destination_options) method is ca... | large_dataset/raw/docs/nodejs/stream/stream_4x.md |
5e8d6311-62f7-5c56-9650-f28c10bab5ad | docs | nodejs | nodejs_stream | 4x | 25 | var writer = getWritableStreamSomehow();
var reader = getReadableStreamSomehow();
writer.on('unpipe', (src) => {
console.error('something has stopped piping into the writer');
assert.equal(src, reader);
});
reader.pipe(writer);
reader.unpipe(writer);
```
#### writable.cork()[#](#stream_writable_cork)
Forces buffe... | large_dataset/raw/docs/nodejs/stream/stream_4x.md |
e4bdbc44-9ae8-5b0f-8cab-e1999ed29e03 | docs | nodejs | nodejs_stream | 4x | 26 | * `callback` [<Function>](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function) Optional callback for when the stream is finished
Call this method when no more data will be written to the stream. If supplied,
the callback is attached as a listener on the [`'finish'`](#stream_event_... | large_dataset/raw/docs/nodejs/stream/stream_4x.md |
d4e31944-b3ad-5f99-ac2c-cd5c2df0520b | docs | nodejs | nodejs_stream | 4x | 27 | #### writable.uncork()[#](#stream_writable_uncork)
Flush all data, buffered since [`stream.cork()`](#stream_writable_cork) call.
#### writable.write(chunk[, encoding][, callback])[#](#stream_writable_write_chunk_encoding_callback)
* `chunk` [<String>](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_stru... | large_dataset/raw/docs/nodejs/stream/stream_4x.md |
fd4f41a0-22a7-560f-875f-84a58fc268a7 | docs | nodejs | nodejs_stream | 4x | 28 | occurs, the callback may or may not be called with the error as its
first argument. To detect write errors, listen for the `'error'` event.
The return value indicates if you should continue writing right now.
If the data had to be buffered internally, then it will return
`false`. Otherwise, it will return `true`.
Thi... | large_dataset/raw/docs/nodejs/stream/stream_4x.md |
fb6129d1-0011-50c4-9bf1-6f93405497d0 | docs | nodejs | nodejs_stream | 4x | 29 | to be sure that the internal mechanisms are set up properly.
3. Implement one or more specific methods, as detailed below.
The class to extend and the method(s) to implement depend on the sort
of stream class you are writing:
| Use-case | Class | Method(s) to implement |
| --- | --- | --- |
| Reading only | [Reada... | large_dataset/raw/docs/nodejs/stream/stream_4x.md |
333b9ed5-7172-5af4-a674-505defeff6fd | docs | nodejs | nodejs_stream | 4x | 30 | ### Class: stream.Duplex[#](#stream_class_stream_duplex_1)
A "duplex" stream is one that is both Readable and Writable, such as a TCP
socket connection.
Note that `stream.Duplex` is an abstract class designed to be extended
with an underlying implementation of the [`stream._read(size)`](#stream_readable_read_size_1)
... | large_dataset/raw/docs/nodejs/stream/stream_4x.md |
6f605aa2-4b86-5d60-997d-8b8cc8f3a1cc | docs | nodejs | nodejs_stream | 4x | 31 | #### new stream.Duplex(options)[#](#stream_new_stream_duplex_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 fields:
+ `allowHalfOpen` [<Boolean>](https://developer.moz... | large_dataset/raw/docs/nodejs/stream/stream_4x.md |
2f7fa1b2-44c6-57e9-a629-93041ef0d69d | docs | nodejs | nodejs_stream | 4x | 32 | In classes that extend the Duplex class, make sure to call the
constructor so that the buffering settings can be properly
initialized.
### Class: stream.PassThrough[#](#stream_class_stream_passthrough)
This is a trivial implementation of a [Transform](#stream_class_stream_transform) stream that simply
passes the inpu... | large_dataset/raw/docs/nodejs/stream/stream_4x.md |
c6c2582f-5114-5e5f-985c-2126904cd694 | docs | nodejs | nodejs_stream | 4x | 33 | #### new stream.Readable([options])[#](#stream_new_stream_readable_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#Number_type) The maximum num... | large_dataset/raw/docs/nodejs/stream/stream_4x.md |
f9c9bb50-de1d-5c79-8619-e3f7c23f805c | docs | nodejs | nodejs_stream | 4x | 34 | + `read` [<Function>](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function) Implementation for the [`stream._read()`](#stream_readable_read_size_1)
method.
In classes that extend the Readable class, make sure to call the
Readable constructor so that the buffering settings can... | large_dataset/raw/docs/nodejs/stream/stream_4x.md |
092d8db0-9905-51a6-9f56-30f8c4605b10 | docs | nodejs | nodejs_stream | 4x | 35 | implementation should start pushing that data into the read queue by calling
[`this.push(dataChunk)`](#stream_readable_push_chunk_encoding). `_read()` should continue reading from
the resource and pushing data until push returns `false`, at which point it
should stop reading from the resource. Only when `_read()` is ca... | large_dataset/raw/docs/nodejs/stream/stream_4x.md |
adf1c5a8-fdfe-591f-a8e7-77827ac8bab1 | docs | nodejs | nodejs_stream | 4x | 36 | `size` bytes are available before calling [`stream.push(chunk)`](#stream_readable_push_chunk_encoding).
#### readable.push(chunk[, encoding])[#](#stream_readable_push_chunk_encoding)
* `chunk` [<Buffer>](buffer.html#buffer_class_buffer) | [<Null>](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structure... | large_dataset/raw/docs/nodejs/stream/stream_4x.md |
54b04005-2768-5561-b82a-71e30b818732 | docs | nodejs | nodejs_stream | 4x | 37 | into the queue for subsequent stream processors to consume. If `null` is
passed, it signals the end of the stream (EOF), after which no more data
can be written.
The data added with `push()` can be pulled out by calling the
[`stream.read()`](#stream_readable_read_size) method when the [`'readable'`](#stream_event_read... | large_dataset/raw/docs/nodejs/stream/stream_4x.md |
9de1a5da-6a4a-5103-aef6-02fc9b744f1a | docs | nodejs | nodejs_stream | 4x | 38 | this._source.ondata = (chunk) => {
// if push() returns false, then we need to stop reading from source
if (!this.push(chunk))
this._source.readStop();
};
// When the source ends, we push the EOF-signaling `null` chunk
this._source.onend = () => {
this.push(null);
};
}
// _read will be cal... | large_dataset/raw/docs/nodejs/stream/stream_4x.md |
8ea3e86e-45ba-5aaf-8ee1-f1a6b3c0a81c | docs | nodejs | nodejs_stream | 4x | 39 | else {
var str = '' + i;
var buf = new Buffer(str, 'ascii');
this.push(buf);
}
};
```
#### Example: SimpleProtocol v1 (Sub-optimal)[#](#stream_example_simpleprotocol_v1_sub_optimal)
This is similar to the `parseHeader` function described
[here](#stream_readable_unshift_chunk), but implemented as a cus... | large_dataset/raw/docs/nodejs/stream/stream_4x.md |
e39f5700-5907-5e5f-9d3f-c96f43c97668 | docs | nodejs | nodejs_stream | 4x | 40 | util.inherits(SimpleProtocol, Readable);
function SimpleProtocol(source, options) {
if (!(this instanceof SimpleProtocol))
return new SimpleProtocol(source, options);
Readable.call(this, options);
this._inBody = false;
this._sawFirstCr = false;
// source is a readable stream, such as a socket or file
... | large_dataset/raw/docs/nodejs/stream/stream_4x.md |
9136bd3d-7319-5a03-bc82-554a02a3e914 | docs | nodejs | nodejs_stream | 4x | 41 | break;
} else {
this._sawFirstCr = true;
}
} else {
this._sawFirstCr = false;
}
}
if (split === -1) {
// still waiting for the \n\n
// stash the chunk, and try again.
this._rawHeader.push(chunk);
this.push('');
} else {
t... | large_dataset/raw/docs/nodejs/stream/stream_4x.md |
e0ce80f7-3ebb-56c7-9719-a7350b981e3f | docs | nodejs | nodejs_stream | 4x | 42 | this.push('');
// and let them know that we are done parsing the header.
this.emit('header', this.header);
}
} else {
// from there on, just provide the data to our consumer.
// careful not to push(null), since that would indicate EOF.
var chunk = this._source.read();
if (chunk)... | large_dataset/raw/docs/nodejs/stream/stream_4x.md |
6831363b-8a6e-527e-bdba-e28af21c5ffc | docs | nodejs | nodejs_stream | 4x | 43 | that is either much smaller or much larger than its input.
Rather than implement the [`stream._read()`](#stream_readable_read_size_1) and
[`stream._write()`](#stream_writable_write_chunk_encoding_callback_1) methods, Transform classes must implement the
[`stream._transform()`](#stream_transform_transform_chunk_encodin... | large_dataset/raw/docs/nodejs/stream/stream_4x.md |
3262b083-9b00-5e87-8469-5f46572be965 | docs | nodejs | nodejs_stream | 4x | 44 | + `flush` [<Function>](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function) Implementation for the [`stream._flush()`](#stream_transform_flush_callback)
method.
In classes that extend the Transform class, make sure to call the
constructor so that the buffering settings can b... | large_dataset/raw/docs/nodejs/stream/stream_4x.md |
a96e2744-c32e-5466-a95d-a1427ea9d71d | docs | nodejs | nodejs_stream | 4x | 45 | #### transform.\_flush(callback)[#](#stream_transform_flush_callback)
* `callback` [<Function>](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function) Call this function (optionally with an error
argument) when you are done flushing any remaining data.
Note: **This function MUST ... | large_dataset/raw/docs/nodejs/stream/stream_4x.md |
260bad01-028d-5982-8b5b-2022ea3f5746 | docs | nodejs | nodejs_stream | 4x | 46 | like with [`stream._transform()`](#stream_transform_transform_chunk_encoding_callback), call
`transform.push(chunk)` zero or more times, as appropriate, and call `callback`
when the flush operation is complete.
This method is prefixed with an underscore because it is internal to
the class that defines it, and should n... | large_dataset/raw/docs/nodejs/stream/stream_4x.md |
cc40babf-5ef7-5e29-ac27-4064ba390e01 | docs | nodejs | nodejs_stream | 4x | 47 | 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-US/docs/Web/JavaScript/Reference/Global_Objects/Function) Call this function (optionally with an error
argument and data) when you are done processing ... | large_dataset/raw/docs/nodejs/stream/stream_4x.md |
a91ea92c-5140-514a-a2f0-64be5f7b755c | docs | nodejs | nodejs_stream | 4x | 48 | Call the callback function only when the current chunk is completely
consumed. Note that there may or may not be output as a result of any
particular input chunk. If you supply a second argument to the callback
it will be passed to the push method. In other words the following are
equivalent:
```None
transform.prototy... | large_dataset/raw/docs/nodejs/stream/stream_4x.md |
c43f3d49-fb21-5c2c-9189-6f9275111b92 | docs | nodejs | nodejs_stream | 4x | 49 | [Transform](#stream_class_stream_transform) stream class, similar to the `parseHeader` and `SimpleProtocol
v1` examples.
In this example, rather than providing the input as an argument, it
would be piped into the parser, which is a more idiomatic Node.js stream
approach.
```None
const util = require('util');
const Tr... | large_dataset/raw/docs/nodejs/stream/stream_4x.md |
21c04080-3e08-500f-ae2c-818dc5234ae7 | docs | nodejs | nodejs_stream | 4x | 50 | } else {
this._sawFirstCr = false;
}
}
if (split === -1) {
// still waiting for the \n\n
// stash the chunk, and try again.
this._rawHeader.push(chunk);
} else {
this._inBody = true;
var h = chunk.slice(0, split);
this._rawHeader.push(h);
var he... | large_dataset/raw/docs/nodejs/stream/stream_4x.md |
d2f64347-e032-5c3d-af07-89f90f431a30 | docs | nodejs | nodejs_stream | 4x | 51 | // with the parsed header data.
```
### Class: stream.Writable[#](#stream_class_stream_writable_1)
`stream.Writable` is an abstract class designed to be extended with an
underlying implementation of the
[`stream._write(chunk, encoding, callback)`](#stream_writable_write_chunk_encoding_callback_1) method.
Please see ... | large_dataset/raw/docs/nodejs/stream/stream_4x.md |
9009ac57-b4a7-578e-88b5-829c54e4a4e3 | docs | nodejs | nodejs_stream | 4x | 52 | + `decodeStrings` [<Boolean>](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type) Whether or not to decode strings into
Buffers before passing them to [`stream._write()`](#stream_writable_write_chunk_encoding_callback_1).
Default = `true`
+ `objectMode` [<Boolean>](https://... | large_dataset/raw/docs/nodejs/stream/stream_4x.md |
e21809b2-21f8-5301-82d1-5a0c54b0fc09 | docs | nodejs | nodejs_stream | 4x | 53 | [`stream._writev()`](#stream_writable_writev_chunks_callback) method.
In classes that extend the Writable class, make sure to call the
constructor so that the buffering settings can be properly
initialized.
#### writable.\_write(chunk, encoding, callback)[#](#stream_writable_write_chunk_encoding_callback_1)
* `c... | large_dataset/raw/docs/nodejs/stream/stream_4x.md |
dd5945a5-b0f8-5054-ad55-7cb1513fce77 | docs | nodejs | nodejs_stream | 4x | 54 | argument) when you are done processing the supplied chunk.
All Writable stream implementations must provide a
[`stream._write()`](#stream_writable_write_chunk_encoding_callback_1) method to send data to the underlying
resource.
Note: **This function MUST NOT be called directly.** It should be
implemented by child c... | large_dataset/raw/docs/nodejs/stream/stream_4x.md |
3cc973d3-dc12-5ce8-87fb-338e55c48fd7 | docs | nodejs | nodejs_stream | 4x | 55 | 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.
#### writable.\_writev(chunks, callback)[#](#stream_writable_writev_chunks_callback)
... | large_dataset/raw/docs/nodejs/stream/stream_4x.md |
38a2ebae-b3f7-56ed-9309-cf00b1d1fa95 | docs | nodejs | nodejs_stream | 4x | 56 | unnecessary. If implemented, it will be called with all the chunks
that are buffered in the write queue.
## Simplified Constructor API[#](#stream_simplified_constructor_api)
In simple cases there is now the added benefit of being able to construct a
stream without inheritance.
This can be done by passing the appropr... | large_dataset/raw/docs/nodejs/stream/stream_4x.md |
f4caa873-5986-53de-ae16-4b7c5f109906 | docs | nodejs | nodejs_stream | 4x | 57 | writev: function(chunks, next) {
// sets this._writev under the hood
// An optional error can be passed as the first argument
next()
}
});
```
### Readable[#](#stream_readable)
```None
var readable = new stream.Readable({
read: function(n) {
// sets this._read under the hood
// push data o... | large_dataset/raw/docs/nodejs/stream/stream_4x.md |
f6b2d2e5-0bee-5afe-9820-b85c5e050889 | docs | nodejs | nodejs_stream | 4x | 58 | ### Writable[#](#stream_writable)
```None
var writable = new stream.Writable({
write: function(chunk, encoding, next) {
// sets this._write under the hood
// An optional error can be passed as the first argument
next()
}
});
// or
var writable = new stream.Writable({
writev: function(chunks, next)... | large_dataset/raw/docs/nodejs/stream/stream_4x.md |
3e93dbb7-343f-5ab0-ab1e-1f8d3acb9e64 | docs | nodejs | nodejs_stream | 4x | 59 | call [`stream.read()`](#stream_readable_read_size), then the data will sit in the internal
queue until it is consumed.
Buffering in Writable streams happens when the user calls
[`stream.write(chunk)`](#stream_writable_write_chunk_encoding_callback) repeatedly, even when it returns `false`.
The purpose of streams, esp... | large_dataset/raw/docs/nodejs/stream/stream_4x.md |
586d5fbe-8ba7-5fe0-bc68-6d66d2085299 | docs | nodejs | nodejs_stream | 4x | 60 | 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 [`stream.pause()`](#stream_readable_pause) method was advisory, rather than
guaranteed. This meant that you still had to be prepared to receive
[`'data'`](#stream_event_data) even... | large_dataset/raw/docs/nodejs/stream/stream_4x.md |
d547bd7c-08b4-5c6e-9eea-3a436d5c57a3 | docs | nodejs | nodejs_stream | 4x | 61 | introduces an edge case in the following conditions:
* No [`'data'`](#stream_event_data) event handler is added.
* The [`stream.resume()`](#stream_readable_resume) method is never called.
* The stream is not piped to any writable destination.
For example, consider the following code:
```None
// WARNING! BROKEN!
net... | large_dataset/raw/docs/nodejs/stream/stream_4x.md |
71a4a514-b21d-56e2-8905-dbac56396108 | docs | nodejs | nodejs_stream | 4x | 62 | // 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 the
[`stream.wrap()`](#stream_readable_wrap_stream) method.
### Object Mode[#](#stream_object_mode)
No... | large_dataset/raw/docs/nodejs/stream/stream_4x.md |
3965a2e9-9f52-5dff-b549-3fe23bb8fa01 | docs | nodejs | nodejs_stream | 4x | 63 | return value from [`stream.read()`](#stream_readable_read_size) indicates that there is no more
data, and [`stream.push(null)`](#stream_readable_push_chunk_encoding) will signal the end of stream data
(`EOF`).
No streams in Node.js core are object mode streams. This pattern is only
used by userland streaming libraries... | large_dataset/raw/docs/nodejs/stream/stream_4x.md |
4b852007-3a11-5405-98bb-5a894ac7121e | docs | nodejs | nodejs_stream | 4x | 64 | if (!(this instanceof JSONParseStream))
return new JSONParseStream();
Transform.call(this, { readableObjectMode : true });
this._buffer = '';
this._decoder = new StringDecoder('utf8');
}
JSONParseStream.prototype._transform = function(chunk, encoding, cb) {
this._buffer += this._decoder.write(chunk);
... | large_dataset/raw/docs/nodejs/stream/stream_4x.md |
9ebb64f4-f2a1-5416-b6ee-c39eff149248 | docs | nodejs | nodejs_stream | 4x | 65 | // push the parsed object out to the readable consumer
this.push(obj);
}
cb();
};
```
### `stream.read(0)`[#](#stream_stream_read_0)
There are some cases where you want to trigger a refresh of the
underlying readable stream mechanisms, without actually consuming any
data. In that case, you can call `strea... | large_dataset/raw/docs/nodejs/stream/stream_4x.md |
691869f8-15a2-5d23-a34b-acd85f507cf2 | docs | nodejs | nodejs_stream | 4x | 66 | [`stream.push()`](#stream_readable_push_chunk_encoding), it will end the `reading` process. However, it
does *not* add any data to the readable buffer, so there's nothing for
a user to consume.
Very rarely, there are cases where you have no data to provide now,
but the consumer of your stream (or, perhaps, another bit... | large_dataset/raw/docs/nodejs/stream/stream_4x.md |
6be10847-16c5-5e44-9bfe-6d18bf58bf7c | docs | nodejs | nodejs_stream | 5x | 0 | # Stream#
Source: https://nodejs.org/docs/latest-v5.x/api/stream.html
Version: 5.x
```None
Stability: 2 - Stable
```
A stream is an abstract interface implemented by various objects in
Node.js. For example a [request to an HTTP server](http.html#http_class_http_incomingmessage) is a
stream, as is [`process.stdout`](... | large_dataset/raw/docs/nodejs/stream/stream_5x.md |
df4172d0-a24b-5d3a-896d-f5a7247f64dd | docs | nodejs | nodejs_stream | 5x | 1 | use if you implement your own custom streams yourself. The API is designed to
make this easy for you to do.
3. The third section goes into more depth about how streams work,
including some of the internal mechanisms and functions that you
should probably not modify unless you definitely know what you are
... | large_dataset/raw/docs/nodejs/stream/stream_5x.md |
a37cfe69-d58b-5d3b-98c5-87d79020b163 | docs | nodejs | nodejs_stream | 5x | 2 | It is not necessary to implement Stream interfaces in order to consume
streams in your programs. If you **are** implementing streaming
interfaces in your own program, please also refer to
[API for Stream Implementors](#stream_api_for_stream_implementors).
Almost all Node.js programs, no matter how simple, use Streams ... | large_dataset/raw/docs/nodejs/stream/stream_5x.md |
a0abf191-ee1a-5ba7-a9cd-f4332380ac2f | docs | nodejs | nodejs_stream | 5x | 3 | } catch (er) {
// uh oh! bad json!
res.statusCode = 400;
return res.end(`error: ${er.message}`);
}
// write back something interesting to the user:
res.write(typeof data);
res.end();
});
});
server.listen(1337);
// $ curl localhost:1337 -d '{}'
// object
// $ curl localhost:1... | large_dataset/raw/docs/nodejs/stream/stream_5x.md |
7f20d559-b38f-5aa6-a28a-4a101ac1468b | docs | nodejs | nodejs_stream | 5x | 4 | A Readable stream will not start emitting data until you indicate that
you are ready to receive it.
Readable streams have two "modes": a **flowing mode** and a **paused
mode**. When in flowing mode, data is read from the underlying system
and provided to your program as fast as possible. In paused mode, you
must expli... | large_dataset/raw/docs/nodejs/stream/stream_5x.md |
fb0ff2bd-513a-5983-a333-0c60a7e22515 | docs | nodejs | nodejs_stream | 5x | 5 | * Calling the [`stream.pipe()`](#stream_readable_pipe_destination_options) method to send the data to a [Writable](#stream_class_stream_writable).
You can switch back to paused mode by doing either of the following:
* If there are no pipe destinations, by calling the
[`stream.pause()`](#stream_readable_pause) metho... | large_dataset/raw/docs/nodejs/stream/stream_5x.md |
2ac17397-5d41-54e8-aaa2-450486dd5702 | docs | nodejs | nodejs_stream | 5x | 6 | * [HTTP requests, on the server](http.html#http_class_http_incomingmessage)
* [fs read streams](fs.html#fs_class_fs_readstream)
* [zlib streams](zlib.html)
* [crypto streams](crypto.html)
* [TCP sockets](net.html#net_class_net_socket)
* [child process stdout and stderr](child_process.html#child_process_child_stdout)
* ... | large_dataset/raw/docs/nodejs/stream/stream_5x.md |
dfa1b3fa-5125-59fd-ba73-79cb2e44f9e4 | docs | nodejs | nodejs_stream | 5x | 7 | explicitly paused will switch the stream into flowing mode. Data will
then be passed as soon as it is available.
If you just want to get all the data out of the stream as fast as
possible, this is the best way to do so.
```None
var readable = getReadableStreamSomehow();
readable.on('data', (chunk) => {
console.log(... | large_dataset/raw/docs/nodejs/stream/stream_5x.md |
65144cc2-a818-5e1b-a29c-41310205fce6 | docs | nodejs | nodejs_stream | 5x | 8 | #### Event: 'error'[#](#stream_event_error)
* [<Error>](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error)
Emitted if there was an error receiving data.
#### Event: 'readable'[#](#stream_event_readable)
When a chunk of data can be read from the stream, it will emit a
`'readable'... | large_dataset/raw/docs/nodejs/stream/stream_5x.md |
6730af7b-b44c-55f4-9b12-f727e7a66d98 | docs | nodejs | nodejs_stream | 5x | 9 | In the former case, [`stream.read()`](#stream_readable_read_size) will return that 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');
var rr = fs.createReadStream('foo.txt');
rr.... | large_dataset/raw/docs/nodejs/stream/stream_5x.md |
4e2d4b24-0b65-5494-b058-5a38eb9bec57 | docs | nodejs | nodejs_stream | 5x | 10 | readable.isPaused() // === true
readable.resume()
readable.isPaused() // === false
```
#### readable.pause()[#](#stream_readable_pause)
* Return: `this`
This method will cause a stream in flowing mode to stop emitting
[`'data'`](#stream_event_data) events, switching out of flowing mode. Any data that becomes
availab... | large_dataset/raw/docs/nodejs/stream/stream_5x.md |
8979ecfa-713d-5d97-8907-42938e01fbcf | docs | nodejs | nodejs_stream | 5x | 11 | + `end` [<Boolean>](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type) End the writer when the reader ends. Default = `true`
This method pulls all the data out of a readable stream, and writes it
to the supplied destination, automatically managing the flow so that
the destination i... | large_dataset/raw/docs/nodejs/stream/stream_5x.md |
0a7e6b5c-ec13-50ab-ba7b-15652f1ccbb2 | docs | nodejs | nodejs_stream | 5x | 12 | By default [`stream.end()`](#stream_writable_end_chunk_encoding_callback) is called on the destination when the
source stream emits [`'end'`](#stream_event_end), so that `destination` is no longer writable.
Pass `{ end: false }` as `options` to keep the destination stream open.
This keeps `writer` open so that "Goodby... | large_dataset/raw/docs/nodejs/stream/stream_5x.md |
46bd44d3-d508-5d36-ba5c-f642a9c4fb2e | docs | nodejs | nodejs_stream | 5x | 13 | * Return [<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 `read()` method pulls some data out of the internal buffer and
returns... | large_dataset/raw/docs/nodejs/stream/stream_5x.md |
038131c0-7d61-5224-8a94-2715eea6f0c1 | docs | nodejs | nodejs_stream | 5x | 14 | console.log('got %d bytes of data', chunk.length);
}
});
```
If this method returns a data chunk, then it will also trigger the
emission of a [`'data'`](#stream_event_data) event.
Note that calling [`stream.read([size])`](#stream_readable_read_size) after the [`'end'`](#stream_event_end)
event has been triggere... | large_dataset/raw/docs/nodejs/stream/stream_5x.md |
aad3d3bc-e522-5725-a125-d183828b022b | docs | nodejs | nodejs_stream | 5x | 15 | #### readable.setEncoding(encoding)[#](#stream_readable_setencoding_encoding)
* `encoding` [<String>](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type) The encoding to use.
* Return: `this`
Call this function to cause the stream to return strings of the specified
encoding instead of... | large_dataset/raw/docs/nodejs/stream/stream_5x.md |
8d3cadaf-a3f3-5765-91d3-8d45c0ec28bf | docs | nodejs | nodejs_stream | 5x | 16 | multi-byte strings spread out over multiple chunks.
```None
var readable = getReadableStreamSomehow();
readable.setEncoding('utf8');
readable.on('data', (chunk) => {
assert.equal(typeof chunk, 'string');
console.log('got %d characters of string data', chunk.length);
});
```
#### readable.unpipe([destination])[#](... | large_dataset/raw/docs/nodejs/stream/stream_5x.md |
9207dc51-0f1f-5a34-975a-67348fddbab2 | docs | nodejs | nodejs_stream | 5x | 17 | readable.unpipe(writable);
console.log('manually close the file stream');
writable.end();
}, 1000);
```
#### readable.unshift(chunk)[#](#stream_readable_unshift_chunk)
* `chunk` [<Buffer>](buffer.html#buffer_class_buffer) | [<String>](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Strin... | large_dataset/raw/docs/nodejs/stream/stream_5x.md |
e83c71c1-78cf-5e74-a810-f5b12100dfae | docs | nodejs | nodejs_stream | 5x | 18 | // Pull off a header delimited by \n\n
// use unshift() if we get too much
// Call the callback with (error, header, stream)
const StringDecoder = require('string_decoder').StringDecoder;
function parseHeader(stream, callback) {
stream.on('error', callback);
stream.on('readable', onReadable);
var decoder = new St... | large_dataset/raw/docs/nodejs/stream/stream_5x.md |
9711e583-3df8-5779-aaaf-577afd7a4b6e | docs | nodejs | nodejs_stream | 5x | 19 | // still reading the header.
header += str;
}
}
}
}
```
Note that, unlike [`stream.push(chunk)`](#stream_readable_push_chunk_encoding), `stream.unshift(chunk)`
will not end the reading process by resetting the internal reading state of the
stream. This can cause unexpected results if `unshi... | large_dataset/raw/docs/nodejs/stream/stream_5x.md |
41de38f4-6046-5a94-8a83-6769c827c468 | docs | nodejs | nodejs_stream | 5x | 20 | entire Streams API as it is today. (See [Compatibility](#stream_compatibility_with_older_node_js_versions) for
more information.)
If you are using an older Node.js library that emits [`'data'`](#stream_event_data) events and
has a [`stream.pause()`](#stream_readable_pause) method that is advisory only, then you
can us... | large_dataset/raw/docs/nodejs/stream/stream_5x.md |
ab09ee03-e943-5d94-9307-a89476ea2e14 | docs | nodejs | nodejs_stream | 5x | 21 | ### Class: stream.Transform[#](#stream_class_stream_transform)
Transform streams are [Duplex](#stream_class_stream_duplex) streams where the output is in some way
computed from the input. They implement both the [Readable](#stream_class_stream_readable) and
[Writable](#stream_class_stream_writable) interfaces.
Exampl... | large_dataset/raw/docs/nodejs/stream/stream_5x.md |
763fdb3e-d2e2-5f87-a47d-79a42ad597e0 | docs | nodejs | nodejs_stream | 5x | 22 | * [`process.stdout`](process.html#process_process_stdout), [`process.stderr`](process.html#process_process_stderr)
#### Event: 'drain'[#](#stream_event_drain)
If a [`stream.write(chunk)`](#stream_writable_write_chunk_encoding_callback) call returns `false`, then the
`'drain'` event will indicate when it is appropriat... | large_dataset/raw/docs/nodejs/stream/stream_5x.md |
f0122471-c347-5e43-81bb-fa3f7fe52293 | docs | nodejs | nodejs_stream | 5x | 23 | writer.once('drain', write);
}
}
}
```
#### Event: 'error'[#](#stream_event_error_1)
* [<Error>](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error)
Emitted if there was an error when writing or piping data.
#### Event: 'finish'[#](#stream_event_finish)
When the [`st... | large_dataset/raw/docs/nodejs/stream/stream_5x.md |
35c80316-eeb9-544d-a1fb-85d0812ee117 | docs | nodejs | nodejs_stream | 5x | 24 | stream, adding this writable to its set of destinations.
```None
var writer = getWritableStreamSomehow();
var reader = getReadableStreamSomehow();
writer.on('pipe', (src) => {
console.error('something is piping into the writer');
assert.equal(src, reader);
});
reader.pipe(writer);
```
#### Event: 'unpipe'[#](#str... | large_dataset/raw/docs/nodejs/stream/stream_5x.md |
d42f2bed-45f1-5af2-a720-821bac2cf202 | docs | nodejs | nodejs_stream | 5x | 25 | #### writable.cork()[#](#stream_writable_cork)
Forces buffering of all writes.
Buffered data will be flushed either at [`stream.uncork()`](#stream_writable_uncork) or at
[`stream.end()`](#stream_writable_end_chunk_encoding_callback) call.
#### writable.end([chunk][, encoding][, callback])[#](#stream_writable_end_chu... | large_dataset/raw/docs/nodejs/stream/stream_5x.md |
549ecd35-d402-5c7e-9fb3-0056c3a3c58e | docs | nodejs | nodejs_stream | 5x | 26 | Calling [`stream.write()`](#stream_writable_write_chunk_encoding_callback) after calling
[`stream.end()`](#stream_writable_end_chunk_encoding_callback) will raise an error.
```None
// write 'hello, ' and then end with 'world!'
var file = fs.createWriteStream('example.txt');
file.write('hello, ');
file.end('world!');
/... | large_dataset/raw/docs/nodejs/stream/stream_5x.md |
fc827d45-0bff-57bb-81fb-1e19dfd820a8 | docs | nodejs | nodejs_stream | 5x | 27 | * `chunk` [<String>](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type) | [<Buffer>](buffer.html#buffer_class_buffer) The data to write
* `encoding` [<String>](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type) The encoding, if `chunk` is a String
* `c... | large_dataset/raw/docs/nodejs/stream/stream_5x.md |
f3a06bc6-0eaa-58dd-bd7a-8b750c8283be | docs | nodejs | nodejs_stream | 5x | 28 | If the data had to be buffered internally, then it will return
`false`. Otherwise, it will return `true`.
This return value is strictly advisory. You MAY continue to write,
even if it returns `false`. However, writes will be buffered in
memory, so it is best not to do this excessively. Instead, wait for
the [`'drain'`... | large_dataset/raw/docs/nodejs/stream/stream_5x.md |
0e0ff224-575a-5001-896d-30a61fa9d4a6 | docs | nodejs | nodejs_stream | 5x | 29 | | Use-case | Class | Method(s) to implement |
| --- | --- | --- |
| Reading only | [Readable](#stream_class_stream_readable_1) | `_read` |
| Writing only | [Writable](#stream_class_stream_writable_1) | `_write`, `_writev` |
| Reading and writing | [Duplex](#stream_class_stream_duplex_1) | `_read`, `_write`, `_writev` |... | large_dataset/raw/docs/nodejs/stream/stream_5x.md |
ca3d742d-dd33-5e1a-9107-fb03de9457d3 | docs | nodejs | nodejs_stream | 5x | 30 | with an underlying implementation of the [`stream._read(size)`](#stream_readable_read_size_1)
and [`stream._write(chunk, encoding, callback)`](#stream_writable_write_chunk_encoding_callback_1) methods as you
would with a Readable or Writable stream class.
Since JavaScript doesn't have multiple prototypal inheritance, ... | large_dataset/raw/docs/nodejs/stream/stream_5x.md |
71c78a0a-fe87-5474-b4ea-a2b814fdfbc1 | docs | nodejs | nodejs_stream | 5x | 31 | + `allowHalfOpen` [<Boolean>](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type) Default = `true`. If set to `false`, then
the stream will automatically end the readable side when the
writable side ends and vice versa.
+ `readableObjectMode` [<Boolean>](https://developer.m... | large_dataset/raw/docs/nodejs/stream/stream_5x.md |
304f327e-088d-522b-bad4-f55b71a4bb3d | docs | nodejs | nodejs_stream | 5x | 32 | ### Class: stream.PassThrough[#](#stream_class_stream_passthrough)
This is a trivial implementation of a [Transform](#stream_class_stream_transform) stream that simply
passes the input bytes across to the output. Its purpose is mainly
for examples and testing, but there are occasionally use cases where
it can come in ... | large_dataset/raw/docs/nodejs/stream/stream_5x.md |
5d7467ca-4527-5f56-9002-e0a33fc63bb8 | docs | nodejs | nodejs_stream | 5x | 33 | #### new stream.Readable([options])[#](#stream_new_stream_readable_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#Number_type) The maximum num... | large_dataset/raw/docs/nodejs/stream/stream_5x.md |
a240c93b-618a-525d-ba53-a5b7fea96531 | docs | nodejs | nodejs_stream | 5x | 34 | + `read` [<Function>](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function) Implementation for the [`stream._read()`](#stream_readable_read_size_1)
method.
In classes that extend the Readable class, make sure to call the
Readable constructor so that the buffering settings can... | large_dataset/raw/docs/nodejs/stream/stream_5x.md |
28f850e1-385c-58ec-9b4d-f0dacf6f7c90 | docs | nodejs | nodejs_stream | 5x | 35 | implementation should start pushing that data into the read queue by calling
[`this.push(dataChunk)`](#stream_readable_push_chunk_encoding). `_read()` should continue reading from
the resource and pushing data until push returns `false`, at which point it
should stop reading from the resource. Only when `_read()` is ca... | large_dataset/raw/docs/nodejs/stream/stream_5x.md |
15bdc653-0176-5883-973a-768e56206b1c | docs | nodejs | nodejs_stream | 5x | 36 | `size` bytes are available before calling [`stream.push(chunk)`](#stream_readable_push_chunk_encoding).
#### readable.push(chunk[, encoding])[#](#stream_readable_push_chunk_encoding)
* `chunk` [<Buffer>](buffer.html#buffer_class_buffer) | [<Null>](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structure... | large_dataset/raw/docs/nodejs/stream/stream_5x.md |
da9d9320-ffdb-5bc3-b2a2-fbc79ba36fe3 | docs | nodejs | nodejs_stream | 5x | 37 | into the queue for subsequent stream processors to consume. If `null` is
passed, it signals the end of the stream (EOF), after which no more data
can be written.
The data added with `push()` can be pulled out by calling the
[`stream.read()`](#stream_readable_read_size) method when the [`'readable'`](#stream_event_read... | large_dataset/raw/docs/nodejs/stream/stream_5x.md |
0c14e576-65cb-527e-a3b7-d7adcf5a6358 | docs | nodejs | nodejs_stream | 5x | 38 | this._source.ondata = (chunk) => {
// if push() returns false, then we need to stop reading from source
if (!this.push(chunk))
this._source.readStop();
};
// When the source ends, we push the EOF-signaling `null` chunk
this._source.onend = () => {
this.push(null);
};
}
// _read will be cal... | large_dataset/raw/docs/nodejs/stream/stream_5x.md |
db83820a-8ad6-544e-ac43-806f4ea07050 | docs | nodejs | nodejs_stream | 5x | 39 | else {
var str = '' + i;
var buf = new Buffer(str, 'ascii');
this.push(buf);
}
};
```
#### Example: SimpleProtocol v1 (Sub-optimal)[#](#stream_example_simpleprotocol_v1_sub_optimal)
This is similar to the `parseHeader` function described
[here](#stream_readable_unshift_chunk), but implemented as a cus... | large_dataset/raw/docs/nodejs/stream/stream_5x.md |
f2838a35-e8a4-5c10-b1c5-e209430a2874 | docs | nodejs | nodejs_stream | 5x | 40 | util.inherits(SimpleProtocol, Readable);
function SimpleProtocol(source, options) {
if (!(this instanceof SimpleProtocol))
return new SimpleProtocol(source, options);
Readable.call(this, options);
this._inBody = false;
this._sawFirstCr = false;
// source is a readable stream, such as a socket or file
... | large_dataset/raw/docs/nodejs/stream/stream_5x.md |
8a3f9e33-cc7d-5a0e-9fe5-d94dc62f35a9 | docs | nodejs | nodejs_stream | 5x | 41 | break;
} else {
this._sawFirstCr = true;
}
} else {
this._sawFirstCr = false;
}
}
if (split === -1) {
// still waiting for the \n\n
// stash the chunk, and try again.
this._rawHeader.push(chunk);
this.push('');
} else {
t... | large_dataset/raw/docs/nodejs/stream/stream_5x.md |
df011221-2b20-562d-a9f6-d4e664eb4b99 | docs | nodejs | nodejs_stream | 5x | 42 | this.push('');
// and let them know that we are done parsing the header.
this.emit('header', this.header);
}
} else {
// from there on, just provide the data to our consumer.
// careful not to push(null), since that would indicate EOF.
var chunk = this._source.read();
if (chunk)... | large_dataset/raw/docs/nodejs/stream/stream_5x.md |
34bed933-2f31-56d2-896c-3a9137f91bbd | docs | nodejs | nodejs_stream | 5x | 43 | that is either much smaller or much larger than its input.
Rather than implement the [`stream._read()`](#stream_readable_read_size_1) and
[`stream._write()`](#stream_writable_write_chunk_encoding_callback_1) methods, Transform classes must implement the
[`stream._transform()`](#stream_transform_transform_chunk_encodin... | large_dataset/raw/docs/nodejs/stream/stream_5x.md |
e4492267-f130-55e8-93e4-f197b7e8c5c1 | docs | nodejs | nodejs_stream | 5x | 44 | + `flush` [<Function>](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function) Implementation for the [`stream._flush()`](#stream_transform_flush_callback)
method.
In classes that extend the Transform class, make sure to call the
constructor so that the buffering settings can b... | large_dataset/raw/docs/nodejs/stream/stream_5x.md |
8b2e9b54-a358-584b-973b-ef1f52c9b83f | docs | nodejs | nodejs_stream | 5x | 45 | #### transform.\_flush(callback)[#](#stream_transform_flush_callback)
* `callback` [<Function>](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function) Call this function (optionally with an error
argument) when you are done flushing any remaining data.
Note: **This function MUST ... | large_dataset/raw/docs/nodejs/stream/stream_5x.md |
299b6832-61a3-5328-aeef-665825616e41 | docs | nodejs | nodejs_stream | 5x | 46 | like with [`stream._transform()`](#stream_transform_transform_chunk_encoding_callback), call
`transform.push(chunk)` zero or more times, as appropriate, and call `callback`
when the flush operation is complete.
This method is prefixed with an underscore because it is internal to
the class that defines it, and should n... | large_dataset/raw/docs/nodejs/stream/stream_5x.md |
9c239fb0-1883-55c7-abd9-b7b5ab6cc261 | docs | nodejs | nodejs_stream | 5x | 47 | 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-US/docs/Web/JavaScript/Reference/Global_Objects/Function) Call this function (optionally with an error
argument and data) when you are done processing ... | large_dataset/raw/docs/nodejs/stream/stream_5x.md |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.