Skip to content

does not work with Node's stream.pipeline() built-in #1

Closed as not planned

Description

@derhuerst

Initial checklist

Affected packages and versions

unified-stream@9, unified-stream@10

Link to runnable example

No response

Steps to reproduce

{
  "private": true,
  "name": "unified-stream-bug",
  "version": "1.0.0",
  "type": "module",
  "main": "index.js",
  "dependencies": {
    "rehype-format": "^4.0.1",
    "rehype-parse": "^8.0.4",
    "rehype-stringify": "^9.0.3",
    "unified": "^10.1.2",
    "unified-stream": "^2.0.0"
  }
}
// index.js
import {pipeline} from 'node:stream'
import {stream} from 'unified-stream'
import {unified} from 'unified'
import rehypeParse from 'rehype-parse'
import rehypeFormat from 'rehype-format'
import rehypeStringify from 'rehype-stringify'

pipeline(
  process.stdin,
  stream(
    unified()
    .use(rehypeParse)
    .use(rehypeFormat)
    .use(rehypeStringify)
  ),
  process.stdout,
  (err) => {
    if (!err) return;
    console.error(err)
    process.exit(1)
  },
)
echo -e 'foo\nbar' | node index.js

<html>
  <head></head>
  <body>
    foo
    bar
  </body>
</html>
file:///Users/j/playground/unified-stream-bug/node_modules/unified-stream/index.js:49
          throw new Error('Did not expect `write` after `end`')
          ^

Error: Did not expect `write` after `end`
    at write (file:///Users/j/playground/unified-stream-bug/node_modules/unified-stream/index.js:49:17)
    at EventEmitter.end (file:///Users/j/playground/unified-stream-bug/node_modules/unified-stream/index.js:90:9)
    at Socket.<anonymous> (node:internal/streams/pipeline:364:11)
    at Object.onceWrapper (node:events:627:28)
    at Socket.emit (node:events:525:35)
    at endReadableNT (node:internal/streams/readable:1359:12)
    at process.processTicksAndRejections (node:internal/process/task_queues:82:21)

Node.js v19.4.0

Expected behavior

As unified-stream (claims to?) returns a duplex Node stream, I would expect to be able to use it in combination with stream.pipeline().

Actual behavior

It fails with an error, see the reproduction steps.

Affected runtime and version

node@19.4.0

Affected package manager and version

No response

Affected OS and version

No response

Build and bundle tools

No response

Activity

  1. added
    👋 phase/newPost is being triaged automatically
    🤞 phase/openPost is being triaged manually
    and removed
    👋 phase/newPost is being triaged automatically
    on Apr 19, 2023
  2. wooorm commented on Apr 20, 2023

    @wooorm
    Member

    Trying that locally on Node 19.8.1, everything works fine.

    $ echo -e 'foo\nbar' | node example.js
    
    <html>
      <head></head>
      <body>
        foo
        bar
      </body>
    </html>
    
    tilde in unified-stream on main [!?]
    $ node -v
    v19.8.1
    

    Can you provide more info on OS/etc? Test more things?

  3. wooorm commented on Apr 20, 2023

    @wooorm
    Member

    I can produce the error on Node 19.3.0 🤔 Weird! Not sure what happened there. But this project has been working for ages!

    And it works again on Node 19.5

  4. wooorm commented on Apr 20, 2023

    @wooorm
    Member

    It looks like this: nodejs/node#46205.
    Apparently there was a bug with Node calling end twice in 17.3+. I also can repro it on 18.2. It seems fixed in 19.5. I also see that it was backported to at least 18.16 (maybe earlier)?

    I think it’s a clear bug in Node. Adding a workaround here for calling end twice to not throw, seems like it might silence more bugs.
    So: recommend you update your engines! :)

  5. github-actions commented on Apr 20, 2023

    @github-actions
  6. added
    👎 phase/noPost cannot or will not be acted on
    and removed
    🤞 phase/openPost is being triaged manually
    on Apr 20, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    👀 no/externalThis makes more sense somewhere else👎 phase/noPost cannot or will not be acted on

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions