Skip to content
This repository has been archived by the owner on Apr 22, 2023. It is now read-only.

Should Stream.pipe() call resume() automatically? (Maybe just for stdin?) #3503

Closed
isaacs opened this issue Jun 21, 2012 · 9 comments
Closed

Comments

@isaacs
Copy link

isaacs commented Jun 21, 2012

It would be nice if this:

process.stdin.pipe(process.stdout);

were equivalent to cat. However, since stdin starts out in a paused state, this does nothing, and you have to do:

process.stdin.pipe(process.stdout);
process.stdin.resume();

which is 100% more lines of code. I find that rather objectionable.

Options:

  1. Stream.prototype.pipe should call this.resume() after setting up the destination. This would be the fix for other streams exhibiting the same problem, but would have much further reaching side effects. I'm very wary about doing this for 0.8, but it could be explored for 0.9.
  2. process.stdin.pipe should call this.resume() after applying Stream.prototype.pipe. This would be the surgical fix, but is perhaps still too hazardous to do now for 0.8 without exploring edge cases.
@isaacs
Copy link
Author

isaacs commented Jun 21, 2012

Opinions from heavy Stream users please: @ry @mikeal @felixge @substack @dominictarr @maxogden

@dominictarr
Copy link

I think this is reasonable, but if it did, it should do so at nextTick at the earliest.
It's possible (likely, in my code) that I'm gonna pipe several.pipe(streams).pipe(together).

It's likely that the first stream is sync (it might just parse or resplit the chunks into lines or something).
so I don't want stdin to start spewing out events until after the last pipe call has returned.

calling resume on the next line is a good bet for this, but hard to remember to do for some streams but not others.

I've written and used streams that start the tick after pipe and found it pretty intuitive.

@max-mapper
Copy link

as long as you can still pause a new steam and do buffering magics then this seems like a nice default behavior

@evlun
Copy link

evlun commented Jun 21, 2012

I'm with @dominictarr on this one.

How about extending the options argument? So, something like:

// this will invoke b.resume() on the next tick
a.pipe(b);

// ...while this won't
a.pipe(b, { 'resume': false });

@mikeal
Copy link

mikeal commented Jun 21, 2012

-1 on adding options.

.pipe() should call resume but resume() MUST do nothing until nextTick(). This way if it is to be paused again before nextTick() it'll go back in to it's paused state.

i think this will also be effected by something isaac and i talked about a while back that "streams should open in a paused state and be resumed before the emit/accept data". this solves the problems we have now with streams essentially opening a closed state until they can emit/accept data.

-Mikeal

On Jun 21, 2012, at June 21, 201211:12 AM, Erik Lundin wrote:

I'm with @dominictarr on this one.

How about extending the options argument? So, something like:

// this will invoke b.resume() on the next tick
a.pipe(b);

// ...while this won't
a.pipe(b, { 'resume': false });

Reply to this email directly or view it on GitHub:
#3503 (comment)

@mikeal
Copy link

mikeal commented Jun 21, 2012

also, I find this less intuitive:

stream.pipe(writable, {resume:false})

than this

stream.pipe(writable)
stream.pause()

The first requires more knowledge about stream internals, the second one matches the intuitive behavior of the public API.

@evlun
Copy link

evlun commented Jun 21, 2012

I agree that the dest.resume() call should be canceled if dest.pause() is invoked before the next tick.

I personally don't think it's too much to assume that the user has some knowledge of how .pipe(dest) works. However, if intuitiveness is a major concern, I don't think the use of dest.pause() will be very straight forward to new users. Using the example provided by @mikeal:

stream.pipe(writable)
stream.pause()

Someone who doesn't know that .pipe(dest) waits a tick before invoking .resume() on the destination object might think that any "buffered" data will be flushed to writable before the stream is paused again.

@isaacs
Copy link
Author

isaacs commented Jun 21, 2012

@mikeal Your "more intuitive" approach would work as expected if stream.pipe(writable) did resume by default. (At least, on process.stdin, which is the only stream in Node that starts out paused.)

@mikeal
Copy link

mikeal commented Jun 21, 2012

@MJor and all.

Note: I think you have a typo, it's the src not the dest that is getting resumed.

I'm making 2 assumptions here:

  1. that .resume() on pipe() is the default behavior. we are presumably doing this because we think it's what users most likely want to do the majority of the time.
  2. that we don't emit events until nextTick() and calling .pause() before nextTick() will cancel the resume and leave the stream in a paused state.

Now, if a user pipes a stream to a destination and notices it begins emitting events I doubt that, without intimate knowledge of the stream implementation, they would assume resume() is getting called as a result of pipe(). They could assume all kinds of behavior: perhaps the stream didn't really open until it was piped or perhaps someone else resumed it.

The user might make any number of assumptions about the underlying implementation and assuming they would search or find or guess that setting {resume:false} would fix their issue seems far fetched. What they are most likely to do is simply pause() the stream.

@isaacs

right, so what happens when that stream opens paused and gets resumed by a pipe call before it is actually open? i doubt it's a spec issue, it's something to think about in implementation tho.

@isaacs isaacs closed this as completed in 5ec0566 Jun 21, 2012
isaacs added a commit to isaacs/node-v0.x-archive that referenced this issue Jul 14, 2012
* V8: upgrade to v3.11.10.10

* npm: Upgrade to 1.1.31

* Deprecate iowatcher (Ben Noordhuis)

* windows: update icon (Bert Belder)

* http: Hush 'MUST NOT have a body' warnings to debug() (isaacs)

* Move blog.nodejs.org content into repository (isaacs)

* Fix nodejs#3503: stdin: resume() on pipe(dest) (isaacs)

* crypto: fix error reporting in SetKey() (Fedor Indutny)

* Add --no-deprecation and --trace-deprecation command-line flags
* (isaacs)

* fs: fix fs.watchFile() (Ben Noordhuis)

* fs: Fix fs.readfile() on pipes (isaacs)

* Rename GYP variable node_use_system_openssl to be consistent (Ryan
* Dahl)
isaacs added a commit to isaacs/node-v0.x-archive that referenced this issue Jul 14, 2012
* V8: upgrade to v3.11.10.10

* npm: Upgrade to 1.1.32

* Deprecate iowatcher (Ben Noordhuis)

* windows: update icon (Bert Belder)

* http: Hush 'MUST NOT have a body' warnings to debug() (isaacs)

* Move blog.nodejs.org content into repository (isaacs)

* Fix nodejs#3503: stdin: resume() on pipe(dest) (isaacs)

* crypto: fix error reporting in SetKey() (Fedor Indutny)

* Add --no-deprecation and --trace-deprecation command-line flags
* (isaacs)

* fs: fix fs.watchFile() (Ben Noordhuis)

* fs: Fix fs.readfile() on pipes (isaacs)

* Rename GYP variable node_use_system_openssl to be consistent (Ryan
* Dahl)
richardlau pushed a commit to ibmruntimes/node that referenced this issue Nov 5, 2015
Notable changes:

* buffer: (Breaking) Removed both 'raw' and 'raws' encoding types from Buffer,
  these have been deprecated for a long time (Sakthipriyan Vairamani) nodejs#2859.
* console: (Breaking) Values reported by console.time() now have 3 decimals of
  accuracy added (Michaël Zasso) nodejs#3166.
* fs:
  - fs.readFile*(), fs.writeFile*(), and fs.appendFile*() now also accept a file
    descriptor as their first argument (Johannes Wüller) nodejs#3163.
  - (Breaking) In fs.readFile(), if an encoding is specified and the internal
    toString() fails the error is no longer thrown but is passed to the callback
    (Evan Lucas) nodejs#3485.
  - (Breaking) In fs.read() (using the fs.read(fd, length, position, encoding,
    callback) form), if the internal toString() fails the error is no longer
    thrown but is passed to the callback (Evan Lucas) nodejs#3503.
* http:
  - Fixed a bug where pipelined http requests would stall (Fedor Indutny) nodejs#3342.
  - (Breaking) When parsing HTTP, don't add duplicates of the following headers:
    Retry-After, ETag, Last-Modified, Server, Age, Expires. This is in addition
    to the following headers which already block duplicates: Content-Type,
    Content-Length, User-Agent, Referer, Host, Authorization,
    Proxy-Authorization, If-Modified-Since, If-Unmodified-Since, From, Location,
    Max-Forwards (James M Snell) nodejs#3090.
  - (Breaking) The callback argument to OutgoingMessage#setTimeout() must be a
    function or a TypeError is thrown (James M Snell) nodejs#3090.
  - (Breaking) HTTP methods and header names must now conform to the RFC 2616
    "token" rule, a list of allowed characters that excludes control characters
    and a number of separator characters. Specifically, methods and header names
    must now match /^[a-zA-Z0-9_!#$%&'*+.^`|~-]+$/ or a TypeError will be thrown
    (James M Snell) nodejs#2526.
* node:
  - (Breaking) Deprecated the _linklist module (Rich Trott) nodejs#3078.
  - (Breaking) Removed require.paths and require.registerExtension(), both had
    been previously set to throw Error when accessed
    (Sakthipriyan Vairamani) nodejs#2922.
* npm: Upgraded to version 3.3.6 from 2.14.7, see
  https://github.com/npm/npm/releases/tag/v3.3.6 for more details. This is a
  major version bump for npm and it has seen a significant amount of change.
  Please see the original npm v3.0.0 release notes for a list of major changes
  (Rebecca Turner) nodejs#3310.
* src: (Breaking) Bumped NODE_MODULE_VERSION to 47 from 46, this is necessary
  due to the V8 upgrade. Native add-ons will need to be recompiled
  (Rod Vagg) nodejs#3400.
* timers: Attempt to reuse the timer handle for setTimeout().unref(). This fixes
  a long-standing known issue where unrefed timers would perviously hold
  beforeExit open (Fedor Indutny) nodejs#3407.
* tls:
  - Added ALPN Support (Shigeki Ohtsu) nodejs#2564.
  - TLS options can now be passed in an object to createSecurePair()
    (Коренберг Марк) nodejs#2441.
  - (Breaking) The default minimum DH key size for tls.connect() is now 1024
    bits and a warning is shown when DH key size is less than 2048 bits. This a security consideration to prevent "logjam" attacks. A new minDHSize TLS
    option can be used to override the default. (Shigeki Ohtsu) nodejs#1831.
* util:
  - (Breaking) util.p() was deprecated for years, and has now been removed
    (Wyatt Preul) nodejs#3432.
  - (Breaking) util.inherits() can now work with ES6 classes. This is considered
    a breaking change because of potential subtle side-effects caused by a
    change from directly reassigning the prototype of the constructor using
    `ctor.prototype = Object.create(superCtor.prototype, { constructor: { ... } })`
    to using `Object.setPrototypeOf(ctor.prototype, superCtor.prototype)`
    (Michaël Zasso) nodejs#3455.
* v8: (Breaking) Upgraded to 4.6.85.25 from 4.5.103.35 (Ali Ijaz Sheikh) nodejs#3351.
  - Implements the spread operator, see
    https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Spread_operator
    for further information.
  - Implements new.target, see
    https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/new.target
    for further information.
* zlib: Decompression now throws on truncated input (e.g. unexpected end of
  file) (Yuval Brik) nodejs#2595.

PR-URL: nodejs/node#3466
richardlau pushed a commit to ibmruntimes/node that referenced this issue Nov 5, 2015
Notable changes:

* buffer: (Breaking) Removed both 'raw' and 'raws' encoding types from Buffer,
  these have been deprecated for a long time (Sakthipriyan Vairamani) nodejs#2859.
* console: (Breaking) Values reported by console.time() now have 3 decimals of
  accuracy added (Michaël Zasso) nodejs#3166.
* fs:
  - fs.readFile*(), fs.writeFile*(), and fs.appendFile*() now also accept a file
    descriptor as their first argument (Johannes Wüller) nodejs#3163.
  - (Breaking) In fs.readFile(), if an encoding is specified and the internal
    toString() fails the error is no longer thrown but is passed to the callback
    (Evan Lucas) nodejs#3485.
  - (Breaking) In fs.read() (using the fs.read(fd, length, position, encoding,
    callback) form), if the internal toString() fails the error is no longer
    thrown but is passed to the callback (Evan Lucas) nodejs#3503.
* http:
  - Fixed a bug where pipelined http requests would stall (Fedor Indutny) nodejs#3342.
  - (Breaking) When parsing HTTP, don't add duplicates of the following headers:
    Retry-After, ETag, Last-Modified, Server, Age, Expires. This is in addition
    to the following headers which already block duplicates: Content-Type,
    Content-Length, User-Agent, Referer, Host, Authorization,
    Proxy-Authorization, If-Modified-Since, If-Unmodified-Since, From, Location,
    Max-Forwards (James M Snell) nodejs#3090.
  - (Breaking) The callback argument to OutgoingMessage#setTimeout() must be a
    function or a TypeError is thrown (James M Snell) nodejs#3090.
  - (Breaking) HTTP methods and header names must now conform to the RFC 2616
    "token" rule, a list of allowed characters that excludes control characters
    and a number of separator characters. Specifically, methods and header names
    must now match /^[a-zA-Z0-9_!#$%&'*+.^`|~-]+$/ or a TypeError will be thrown
    (James M Snell) nodejs#2526.
* node:
  - (Breaking) Deprecated the _linklist module (Rich Trott) nodejs#3078.
  - (Breaking) Removed require.paths and require.registerExtension(), both had
    been previously set to throw Error when accessed
    (Sakthipriyan Vairamani) nodejs#2922.
* npm: Upgraded to version 3.3.6 from 2.14.7, see
  https://github.com/npm/npm/releases/tag/v3.3.6 for more details. This is a
  major version bump for npm and it has seen a significant amount of change.
  Please see the original npm v3.0.0 release notes for a list of major changes
  (Rebecca Turner) nodejs#3310.
* src: (Breaking) Bumped NODE_MODULE_VERSION to 47 from 46, this is necessary
  due to the V8 upgrade. Native add-ons will need to be recompiled
  (Rod Vagg) nodejs#3400.
* timers: Attempt to reuse the timer handle for setTimeout().unref(). This fixes
  a long-standing known issue where unrefed timers would perviously hold
  beforeExit open (Fedor Indutny) nodejs#3407.
* tls:
  - Added ALPN Support (Shigeki Ohtsu) nodejs#2564.
  - TLS options can now be passed in an object to createSecurePair()
    (Коренберг Марк) nodejs#2441.
  - (Breaking) The default minimum DH key size for tls.connect() is now 1024
    bits and a warning is shown when DH key size is less than 2048 bits. This a security consideration to prevent "logjam" attacks. A new minDHSize TLS
    option can be used to override the default. (Shigeki Ohtsu) nodejs#1831.
* util:
  - (Breaking) util.p() was deprecated for years, and has now been removed
    (Wyatt Preul) nodejs#3432.
  - (Breaking) util.inherits() can now work with ES6 classes. This is considered
    a breaking change because of potential subtle side-effects caused by a
    change from directly reassigning the prototype of the constructor using
    `ctor.prototype = Object.create(superCtor.prototype, { constructor: { ... } })`
    to using `Object.setPrototypeOf(ctor.prototype, superCtor.prototype)`
    (Michaël Zasso) nodejs#3455.
* v8: (Breaking) Upgraded to 4.6.85.25 from 4.5.103.35 (Ali Ijaz Sheikh) nodejs#3351.
  - Implements the spread operator, see
    https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Spread_operator
    for further information.
  - Implements new.target, see
    https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/new.target
    for further information.
* zlib: Decompression now throws on truncated input (e.g. unexpected end of
  file) (Yuval Brik) nodejs#2595.

PR-URL: nodejs/node#3466
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

5 participants