0
0
mirror of https://github.com/nodejs/node.git synced 2024-11-30 07:27:22 +01:00
Commit Graph

128 Commits

Author SHA1 Message Date
Calvin Metcalf
e7c077c610 stream: make null an invalid chunk to write in object mode
this harmonizes behavior between readable, writable, and transform
streams so that they all handle nulls in object mode the same way by
considering them invalid chunks.

PR-URL: https://github.com/nodejs/node/pull/6170
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
2016-04-20 13:28:35 -07:00
Matteo Collina
0b1d89f35a streams: support unlimited synchronous cork/uncork cycles
net streams can request multiple chunks to be written in a synchronous
fashion. If this is combined with cork/uncork, en error is currently
thrown because of a regression introduced in:
89aeab901a
(https://github.com/nodejs/node/pull/4354).

Fixes: https://github.com/nodejs/node/issues/6154
PR-URL: https://github.com/nodejs/node/pull/6164
Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com>
Reviewed-By: Mathias Buus <mathiasbuus@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
2016-04-14 08:30:32 +02:00
James M Snell
85ab4a5f12 buffer: add .from(), .alloc() and .allocUnsafe()
Several changes:

* Soft-Deprecate Buffer() constructors
* Add `Buffer.from()`, `Buffer.alloc()`, and `Buffer.allocUnsafe()`
* Add `--zero-fill-buffers` command line option
* Add byteOffset and length to `new Buffer(arrayBuffer)` constructor
* buffer.fill('') previously had no effect, now zero-fills
* Update the docs

PR-URL: https://github.com/nodejs/node/pull/4682
Reviewed-By: Сковорода Никита Андреевич <chalkerx@gmail.com>
Reviewed-By: Stephen Belanger <admin@stephenbelanger.com>
2016-03-16 08:34:02 -07:00
Matteo Collina
7764b6cb96 streams: 5% throughput gain when sending small chunks
Improves the performance when moving small buffers by 5%,
and it adds a benchmark to avoid regression in that area.
In all other cases it is equally performant to current master.

Full performance results available at:
https://gist.github.com/mcollina/717c35ad07d15710b6b9.

PR-URL: https://github.com/nodejs/node/pull/4354
Reviewed-By: James M Snell <jasnell@gmail.com>
2016-02-09 09:30:12 +00:00
Brian White
cc0342a517 streams: update .readable/.writable to false
These properties were initially used to determine stream status
back in node v0.8 and earlier. Since streams2 however, these
properties were *always* true, which can be misleading for
example if you are trying to immediately determine whether
a Writable stream is still writable or not (to avoid a "write after
end" exception).

PR-URL: https://github.com/nodejs/node/pull/4083
Reviewed-By: Chris Dickinson <christopher.s.dickinson@gmail.com>
2015-12-02 12:56:48 -05:00
micnic
20285ad177 lib: Consistent error messages in all modules
This commit fixes some error messages that are not consistent with
some general rules which most of the error messages follow.

PR-URL: https://github.com/nodejs/node/pull/3374
Reviewed-By: Roman Reiss <me@silverwind.io>
2015-11-09 20:08:36 +01:00
Fábio Santos
85b74de9de stream: fix signature of _write() in a comment
This comment was a bit misleading, since it was missing the `encoding`
argument.

PR-URL: https://github.com/nodejs/node/pull/3248
Reviewed-By: Brendan Ashworth <brendan.ashworth@me.com>
2015-10-13 22:32:21 -07:00
Sakthipriyan Vairamani
9cd44bb2b6 util: prepend '(node) ' to deprecation messages
Changes included in this commit are

   1. Making the deprecation messages consistent. The messages will be in
      the following format

           x is deprecated. Use y instead.

      If there is no alternative for `x`, then the ` Use y instead.` part
      will not be there in the message.

   2. All the internal deprecation messages are printed with the prefix
      `(node) `, except when the `--trace-deprecation` flag is set.

Fixes: https://github.com/nodejs/io.js/issues/1883
PR-URL: https://github.com/nodejs/io.js/pull/1892
Reviewed-By: Roman Reiss <me@silverwind.io>
2015-07-03 16:32:29 +02:00
Roman Reiss
b5b8ff117c lib: don't use global Buffer
Port of https://github.com/joyent/node/pull/8603

The race condition present in the original PR didn't occur, so no
workaround was needed.

PR-URL: https://github.com/nodejs/io.js/pull/1794
Reviewed-By: Trevor Norris <trev.norris@gmail.com>
2015-06-11 20:24:44 +02:00
Brian White
5abd4ac079 lib: simplify nextTick() usage
This commit removes unnecessary nextTick() closures and adds some
shared nextTick() callbacks for better re-use.

PR-URL: https://github.com/nodejs/io.js/pull/1612
Reviewed-By: Yosuke Furukawa <yosuke.furukawa@gmail.com>
Reviewed-By: Brendan Ashworth <brendan.ashworth@me.com>
2015-05-25 10:14:18 -04:00
Yosuke Furukawa
19ffb5cf1c lib: fix eslint styles
PR-URL: https://github.com/iojs/io.js/pull/1539
Fixes: https://github.com/iojs/io.js/issues/1253
Reviewed-By: Jeremiah Senkpiel <fishrock123@rocketmail.com>
Reviewed-By: Trevor Norris <trev.norris@gmail.com>
Reviewed-By: Roman Reiss <me@silverwind.io>
Reviewed-By: Chris Dickinson <christopher.s.dickinson@gmail.com>
Reviewed-By: Johan Bergström <bugs@bergstroem.nu>
Reviewed-By: Fedor Indutny <fedor.indutny@gmail.com>
2015-05-09 12:10:02 +09:00
Trevor Norris
10e31ba56c node: allow multiple arguments passed to nextTick
PR-URL: https://github.com/iojs/io.js/pull/1077
Reviewed-by: Colin Ihrig <cjihrig@gmail.com>
2015-04-15 17:02:21 -06:00
Brian White
4dc6ae2181 lib: remove unused variables
PR-URL: https://github.com/iojs/io.js/pull/1290
Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl>
2015-03-31 15:58:42 -04:00
Sam Newman
50daee7243 stream: simpler stream constructon
Adds simplified constructor pattern, allowing users
to provide "read", "write", "transform", "flush", and
"writev" functions as stream options in lieu of subclassing.

Semver: minor
PR-URL: https://github.com/iojs/io.js/pull/697
Fixes: https://github.com/iojs/readable-stream/issues/102
Reviewed-By: Chris Dickinson <christopher.s.dickinson@gmail.com>
2015-02-04 20:19:39 -08:00
cjihrig
6ac8bdc0ab lib: reduce util.is*() usage
Many of the util.is*() methods used to check data types
simply compare against a single value or the result of
typeof. This commit replaces calls to these methods with
equivalent checks. This commit does not touch calls to the
more complex methods (isRegExp(), isDate(), etc.).

Fixes: https://github.com/iojs/io.js/issues/607
PR-URL: https://github.com/iojs/io.js/pull/647
Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl>
2015-01-31 23:47:29 -05:00
cjihrig
40ffed8f3f stream: use nop as write() callback if omitted
This commit introduces a nop function that is used as the
Writable.prototype.write() callback when one is not provided.
This saves on function object creation.

PR-URL: https://github.com/iojs/io.js/pull/564
Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl>
Reviewed-By: Jeremiah Senkpiel <fishrock123@rocketmail.com>
2015-01-23 09:43:31 -05:00
cjihrig
804e7aa9ab lib: use const to define constants
This commit replaces a number of var statements throughout
the lib code with const statements.

PR-URL: https://github.com/iojs/io.js/pull/541
Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl>
2015-01-21 16:21:31 -05:00
isaacs
3e1b1dd4a9 Remove excessive copyright/license boilerplate
The copyright and license notice is already in the LICENSE file.  There
is no justifiable reason to also require that it be included in every
file, since the individual files are not individually distributed except
as part of the entire package.
2015-01-12 15:30:28 -08:00
Ben Noordhuis
94e147500c Merge remote-tracking branch 'joyent/v0.12' into v1.x
I was originally going to do this after the v0.11.15 release, but as
that release is three weeks overdue now, I decided not to wait any
longer; we don't want the delta to get too big.

Conflicts:
	lib/net.js
	test/simple/simple.status

PR-URL: https://github.com/iojs/io.js/pull/236
Reviewed-By: Bert Belder <bertbelder@gmail.com>
Reviewed-By: Fedor Indutny <fedor@indutny.com>
2015-01-05 17:26:47 +01:00
Chris Dickinson
91586661c9 stream: switch _writableState.buffer to queue
In cases where many small writes are made to a stream
lacking _writev, the array data structure backing the
WriteReq buffer would greatly increase GC pressure.

Specifically, in the fs.WriteStream case, the
clearBuffer routine would only clear a single WriteReq
from the buffer before exiting, but would cause the
entire backing array to be GC'd. Switching to [].shift
lessened pressure, but still the bulk of the time was
spent in memcpy.

This replaces that structure with a linked list-backed
queue so that adding and removing from the queue is O(1).
In the _writev case, collecting the buffer requires an
O(N) loop over the buffer, but that was already being
performed to collect callbacks, so slowdown should be
neglible.

PR-URL: https://github.com/joyent/node/pull/8826
Reviewed-by: Timothy J Fontaine <tjfontaine@gmail.com>
Reviewed-by: Trevor Norris <trev.norris@gmail.com>
2014-12-18 09:39:05 -08:00
Ben Noordhuis
21130c7d6f lib: turn on strict mode
Turn on strict mode for the files in the lib/ directory.  It helps
catch bugs and can have a positive effect on performance.

PR-URL: https://github.com/node-forward/node/pull/64
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: Fedor Indutny <fedor@indutny.com>
2014-11-22 17:23:30 +01:00
Brian White
874dd590cf streams: make setDefaultEncoding() throw
PR-URL: https://github.com/joyent/node/pull/8529
Fixes: f04f3a0 "streams: set default encoding for writable streams"
[trev.norris@gmail.com: update tests to check if throws]
Signed-off-by: Trevor Norris <trev.norris@gmail.com>
2014-10-09 16:48:59 -07:00
Johnny Ray
f04f3a0d01 streams: set default encoding for writable streams
Add API Writable#setDefaultEncoding().

PR-URL: https://github.com/joyent/node/pull/8483
Fixes: https://github.com/joyent/node/issues/7159
Reviewed-by: Trevor Norris <trev.norris@gmail.com>
2014-10-07 17:04:10 -07:00
Andrew Oppenlander
e1fec22f97 streams: set default hwm properly for Duplex
Default highWaterMark is now set properly when using stream Duplex's
writableObjectMode and readableObjectMode options.

Added condition to the already existing split objectMode test to ensure
the highWaterMark is being set to the correct default value on both the
ReadableState and WritableState for readableObjectMode and
writableObjectMode.

Signed-off-by: Fedor Indutny <fedor@indutny.com>
2014-07-10 13:28:55 +03:00
Vladimir Kurchatkin
82fca9136d stream: don't try to finish if buffer is not empty
fixes #5715
2014-05-06 06:50:25 -07:00
Vladimir Kurchatkin
d0fc5538d1 stream: split objectMode for Duplex
This commit introduces `readableObjectMode` and
`writableObjectMode` options for Duplex streams.
This can be used mostly to make parsers and
serializers with Transform streams.

Also the docs section about stream state objects
is removed, because it is not relevant anymore.
The example from the section is remade to show
new options.

fixes #6284

Signed-off-by: Timothy J Fontaine <tjfontaine@gmail.com>
2014-04-30 10:44:31 -07:00
Timothy J Fontaine
d58c206862 Merge remote-tracking branch 'upstream/v0.10'
Conflicts:
	lib/_stream_writable.js
2014-02-10 11:21:09 -08:00
Fedor Indutny
e2a1d9a9ac stream: use errorEmitted from _writableState 2014-02-10 11:06:03 -08:00
Fedor Indutny
dee5270a6c net: do not re-emit stream errors
fix #7015
2014-02-10 10:59:52 -08:00
Fedor Indutny
d019eac5b5 tls: emit clientError on early socket close
fix #6903
2014-01-24 22:09:17 +04:00
Timothy J Fontaine
429b58701a Merge remote-tracking branch 'upstream/v0.10'
Conflicts:
	deps/uv/ChangeLog
	deps/uv/build.mk
	deps/uv/src/version.c
	deps/uv/test/test-ipc.c
	deps/v8/src/objects.cc
	src/node.cc
	src/node_os.cc
2014-01-13 14:56:12 -08:00
ayanamist
b922b5e90d stream: writes may return false but forget to emit drain
If a write is above the highWaterMark, _write still manages to
fully send it synchronously, _writableState.length will be adjusted down
to 0 synchronously with the write returning false, but 'drain' will
not be emitted until process.nextTick.

If another small write which is below highWaterMark is issued before
process.nextTick happens, _writableState.needDrain will be reset to false,
and the drain event will never be fired.

So we should check needDrain before setting it up, which prevents it
from inproperly resetting to false.
2014-01-05 19:44:45 +04:00
Mathias Buus
ba72570eae stream: change default hwm for objectMode to 16 2013-08-27 18:02:30 -07:00
isaacs
a3da3e7312 stream: Pass 'buffer' encoding to decoded writables
Since the encoding is no longer relevant once it is decoded to a Buffer,
it is confusing and incorrect to pass the encoding as 'utf8' or whatever
in those cases.

Closes #6119
2013-08-27 14:53:06 -07:00
Matthew Aynalem
c171c490f2 fixes #6031 spelling errors
explictly => explicitly
accesss => access
througput => throughput
epxression => expression
communiction => communication
becuase => because
repersent => represent
condonitions => conditions
decompresion => decompression
intentially => intentionally
eventes => events
listning => listening
resicved => received
becuase => because
fundimental => fundamental
colapse => collapse
privlages => privileges
sufficently => sufficiently
hapepns => happens
expliclitly => explicitly
thier => their
shold => should
2013-08-19 16:42:16 -07:00
Brian White
6d842897c5 lib: remove unused variables and functions 2013-08-15 17:19:17 -07:00
isaacs
22c68fdc1d src: Replace macros with util functions 2013-08-01 15:08:01 -07:00
Ben Noordhuis
0330bdf519 lib: macro-ify type checks
Increases the grep factor. Makes it easier to harmonize type checks
across the code base.
2013-07-24 21:49:35 +02:00
isaacs
61c9f78c63 Merge remote-tracking branch 'ry/v0.10' into master
Conflicts:
	AUTHORS
	ChangeLog
	deps/uv/ChangeLog
	deps/uv/config-unix.mk
	deps/uv/src/unix/stream.c
	deps/uv/src/version.c
	deps/uv/uv.gyp
	src/node.cc
	src/node_buffer.cc
	src/node_crypto.cc
	src/node_version.h
	src/stream_wrap.cc
	src/stream_wrap.h
2013-05-17 14:04:54 -07:00
isaacs
d5158574c6 stream: Make default encoding configurable
Pretty much everything assumes strings to be utf-8, but crypto
traditionally used binary strings, so we need to keep the default
that way until most users get off of that pattern.
2013-05-14 11:36:05 -07:00
isaacs
a58454226f stream: Handle multi-corking properly
This adds proper support for the following situation:

    w.cork();
    w.write(...);
    w.cork();
    w.write(...);
    w.uncork();
    w.write(...);
    w.uncork();

This is relevant when you have a function (as we do in HTTP) that wants
to use cork, but in some cases, want to have a cork/uncork *around*
that function, without losing the benefits of writev.
2013-05-09 09:35:32 -07:00
isaacs
c38ce9bc0a stream: Guarantee ordering of 'finish' event
In synchronous Writable streams (where the _write cb is called on the
current tick), the 'finish' event (and thus the end() callback) can in
some cases be called before all the write() callbacks are called.

Use a counter, and have stream.Transform rely on the 'prefinish' event
instead of the 'finish' event.

This has zero effect on most streams, but it corrects an edge case and
makes it perform more deterministically, which is a Good Thing.
2013-05-09 09:35:32 -07:00
Fedor Indutny
21ed8df696 streams: introduce .cork/.uncork/._writev 2013-04-27 15:59:13 +04:00
isaacs
c93af860a0 stream: call write cb before finish event
Since 049903e, an end callback could be called before a write
callback if end() is called before the write is done. This patch
resolves the issue.

In collaboration with @gne

Fixes felixge/node-formidable#209
Fixes #5215
2013-04-09 02:09:51 +04:00
isaacs
c0d500102a stream: Fix early end in Writables on zero-length writes
Doing this causes problems:

    z.write(Buffer(0));
    z.end();

Fix by not ending Writable streams while they're still in the process of
writing something.
2013-03-24 14:23:21 -07:00
isaacs
e0cec37d50 stream: Remove unnecessary nextTick usage in Writable
Fix #4928
2013-03-06 11:44:29 -08:00
isaacs
312289b791 stream: Use class for write buffer entries 2013-03-05 14:27:16 -08:00
isaacs
426b4c6258 stream: _write takes an encoding argument
This vastly reduces the overhead of decodeStrings:false streams,
such as net and http.
2013-03-05 14:27:15 -08:00
isaacs
049903e333 stream: Split Writable logic into small functions
1. Get rid of unnecessary 'finishing' flag
2. Dont check both ending and ended. Extraneous.

Also: Remove extraneous 'finishing' flag, and don't check both 'ending'
and 'ended', since checking just 'ending' is sufficient.
2013-03-05 14:26:34 -08:00
isaacs
384f1be739 stream: Writable.end(chunk) after end is an error
Calling end(data) calls write(data).  Doing this after end should
raise a 'write after end' error.

However, because end() calls were previously ignored on already
ended streams, this error was confusingly suppressed, even though the
data never is written, and cannot get to the other side.

This is a re-hash of 5222d19a11, but
without assuming that the data passed to end() is valid, and thus
breaking a bunch of tests.
2013-03-03 17:26:38 -08:00
Ben Noordhuis
80ea63b958 Revert "stream: Writable.end(chunk) after end is an error"
It's breaking ~22 tests, Needs further investigation.

This reverts commit 5222d19a11.
2013-03-03 02:21:28 +01:00
isaacs
5222d19a11 stream: Writable.end(chunk) after end is an error
Calling end(data) calls write(data).  Doing this after end should
raise a 'write after end' error.

However, because end() calls were previously ignored on already
ended streams, this error was confusingly suppressed, even though the
data never is written, and cannot get to the other side.
2013-03-02 16:09:16 -08:00
isaacs
2106ef000c net: Provide better error when writing after FIN
The stock writable stream "write after end" message is overly vague, if
you have clearly not called end() yourself yet.

When we receive a FIN from the other side, and call destroySoon() as a
result, then generate an EPIPE error (which is what would happen if you
did actually write to the socket), with a message explaining what
actually happened.
2013-03-02 11:26:39 -08:00
isaacs
86433979c6 stream: Writables are not pipe()able
This handles the fact that stream.Writable inherits from the Stream class,
meaning that it has the legacy pipe() method.  Override that with a pipe()
method that emits an error.

Ensure that Duplex streams ARE still pipe()able, however.

Since the 'readable' flag on streams is sometimes temporary, it's probably
better not to put too much weight on that.  But if something is an instanceof
Writable, rather than of Readable or Duplex, then it's safe to say that
reading from it is the wrong thing to do.

Fix #3647
2013-02-26 18:54:05 -08:00
isaacs
34046084c0 stream: Do not switch to objectMode implicitly
Only handle objects if explicitly told to do so in the options
object.  Non-buffer/string chunks are an error if not already in
objectMode.

Close #4662
2013-02-25 07:38:10 -08:00
isaacs
e03bc472f0 stream: Start out in sync=true state
The Readable and Writable classes will nextTick certain things
if in sync mode.  The sync flag gets unset after a call to _read
or _write.  However, most of these behaviors should also be
deferred until nextTick if no reads have been made (for example,
the automatic '_read up to hwm' behavior on Readable.push(chunk))

Set the sync flag to true in the constructor, so that it will not
trigger an immediate 'readable' event, call to _read, before the
user has had a chance to set a _read method implementation.
2013-02-25 07:38:10 -08:00
isaacs
3b2e9d2648 stream: remove lowWaterMark feature
It seems like a good idea on the face of it, but lowWaterMarks are
actually not useful, and in practice should always be set to zero.

It would be worthwhile for writers if we actually did some kind of
writev() type of thing, but actually this just delays calling write()
and the overhead of doing a bunch of Buffer copies is not worth the
slight benefit of calling write() fewer times.
2013-02-21 15:23:18 -08:00
isaacs
33b2aebb6d stream: Writable should ignore encoding for buffers
Fix #4727
Fix einaros/ws#159
2013-02-07 08:50:18 -08:00
Nathan Rajlich
a9c4a20331 stream: make Writable#end() accept a callback function
This is more backwards-compatible with stream1 streams like `fs.WriteStream`
which would allow a callback function to be passed in as the only argument.

Closes #4719.
2013-02-05 15:12:06 -08:00
Raynos
444bbd4fa7 streams: Support objects other than Buffers
We detect for non-string and non-buffer values in onread and
turn the stream into an "objectMode" stream.

If we are in "objectMode" mode then howMuchToRead will
always return 1, state.length will always have 1 appended
to it when there is a new item and fromList always takes
the first value from the list.

This means that for object streams, the n in read(n) is
ignored and read() will always return a single value

Fixed a bug with unpipe where the pipe would break because
the flowing state was not reset to false.

Fixed a bug with sync cb(null, null) in _read which would
forget to end the readable stream
2013-01-24 07:49:27 -08:00
isaacs
20a3c5d09c streams2: Do not allow hwm < lwm
There was previously an assert() in there, but this part of the code is
so high-volume that the added cost made a measurable dent in http_simple.

Just checking inline is fine, though, and prevents a lot of potential
hazards.
2013-01-14 16:03:38 -08:00
isaacs
f9caf7020c streams: Speed up by doing less work in the state ctors 2012-12-21 00:07:34 +00:00
isaacs
9f4c0988c3 streams2: Process write buffer in a loop, not recursively
This fixes pummel/test-net-write-callbacks
2012-12-19 10:55:23 -08:00
isaacs
f63af64eb8 test-pummel: Add call validation in net-write-callbacks 2012-12-19 10:55:23 -08:00
isaacs
82c7c84e25 net: Handle sync writable streams synchronously
This fixes the case where stderr doesn't flush before the process exits.
2012-12-17 15:08:57 -08:00
isaacs
3751c0fe40 streams2: Still emit error if there was a write() cb 2012-12-14 17:46:23 -08:00
isaacs
5760244cc6 streams2: Writable only emit 'finish' once 2012-12-14 10:52:28 -08:00
isaacs
b15e19a232 streams2: Remove function.bind() usage
It's too slow, unfortunately.
2012-12-13 17:00:32 -08:00
isaacs
0118584433 streams2: Writable organization, add 'finishing' flag 2012-12-13 17:00:31 -08:00
isaacs
63ac07b32b streams2: Export Readable/Writable State classes 2012-12-13 17:00:30 -08:00
isaacs
e82d06bef9 streams2: Fix regression from Duplex ctor assignment 2012-12-13 17:00:28 -08:00
isaacs
71e2b61388 streams2: Support write(chunk,[encoding],[callback]) 2012-12-13 17:00:27 -08:00
isaacs
0678480b57 streams2: Allow Writables to opt out of pre-buffer-izing 2012-12-13 17:00:27 -08:00
isaacs
545f512619 streams2: ctor guards on Stream classes 2012-12-13 17:00:26 -08:00
isaacs
06e321d0f9 streams2: Correct drain/return logic
It was testing the length *before* adding the current chunk, which
is the opposite of correct.

Also, the return value was flipped.
2012-12-13 17:00:25 -08:00
isaacs
02f017d24f streams2: Allow 0 as a lowWaterMark value 2012-12-13 17:00:25 -08:00
isaacs
639fbe28d1 streams2: Convert strings to buffers before passing to _write() 2012-12-13 17:00:23 -08:00
isaacs
420e07c577 streams2: The new stream base classes 2012-12-13 17:00:23 -08:00