`require('stream').promises` is an empty object when `stream/promises` is loaded before `stream`

Đang mở
#67,909 0 bình luận 0 reaction 0 người được giao Xem trên GitHub

Maintainer thường phản hồi trong vòng 1 ngày

Chưa có ai nhận issue này.

Đánh giá

Độ khó
3/5
Thời gian dự kiến
1-2 ngày
Mức phù hợp với người mới
58/100
Loại issue
Lỗi
Độ rõ ràng
Đặc tả rõ ràng
Mức độ hoạt động
Sôi nổi
Công nghệ
javascript, node.js
Lĩnh vực
backend, testing

Hướng nghiên cứu

The issue identifies the cycle in lib/stream/promises.js and lib/stream.js; first read those files and inspect the lazy loading change in #66311. Add a regression test that loads stream/promises before stream and checks the expected shared exports, including the behavior described for util.promisify. Done when the test passes in both load orders.

Do mô hình lập chỉ mục viết ra từ nội dung của issue.

Mô tả

Version

v26.13.1, v24.22.3 and v22.23.5

Platform
Linux x64 and Windows x64 (official binaries)
Subsystem

stream

What steps will reproduce the bug?
$ node -e "require('stream/promises'); console.log(Object.keys(require('stream').promises))"
[]

With stream loaded first it is fine:

$ node -e "require('stream'); require('stream/promises'); console.log(Object.keys(require('stream').promises))"
[ 'finished', 'pipeline' ]
How often does it reproduce? Is there a required condition?

Always, when stream/promises is loaded before anything has loaded stream. ESM import
and the node: specifiers behave the same.

Anything that loads stream first hides it (console.log, net, http, ...), which is
probably why it has gone unnoticed. We hit it in an application whose entry point starts
with import { pipeline } from 'node:stream/promises' while a dependency calls
stream.promises.pipeline.

What is the expected behavior? Why is that the expected behavior?

require('stream').promises === require('stream/promises') in either load order.

What do you see instead?

require('stream').promises is a different, empty object for the life of the process, so
stream.promises.pipeline(...) throws TypeError: stream.promises.pipeline is not a function.

util.promisify(stream.pipeline) is affected too: it silently returns a generic wrapper
instead of the stream/promises function, and that wrapper rejects an options object
such as { signal }.

Additional information

Cause. stream and stream/promises require each other (line numbers from v24.22.3):

  • lib/stream/promises.js:17 runs require('stream') before it sets module.exports
  • lib/stream.js:51 runs const promises = require('stream/promises') and captures the
    still-empty exports object
  • lib/stream/promises.js:41 then replaces module.exports, which stream never sees

It takes two changes together: #46147 added the require('stream') line, and #45659
stopped stream being loaded during bootstrap.

Status on main. #66311 ("worker: preload streams into startup snapshot") makes
lib/stream.js load stream/promises lazily, which removes the cycle. It is a worker
startup change: it does not mention this behavior and adds no test for it, and it is not
on the v22.x, v24.x or v26.x branches.

Ngôn ngữ chính
JavaScript
Star
124k
Fork
38k
Merge trung bình
4 ngày 2 giờ
Pull request đã merge (30 ngày)
266

Chuẩn bị môi trường

Bắt đầu từ đâu

  1. Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
  2. Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
  3. Fork repository và làm thay đổi trên một nhánh.
  4. Mở pull request có tham chiếu số hiệu của issue.

Issue khác của nodejs/node

Tất cả issue của nodejs/node

Issue tương tự

Thêm issue về JavaScript

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.