Skip to content

Conversation

@tt-a1i
Copy link

@tt-a1i tt-a1i commented Dec 31, 2025

Summary

Implements FileHandle.readableWebStream() method for Node.js compatibility.

This method returns a Web Streams API ReadableStream that can be used to read the file contents. It supports the autoClose option which, when true, will automatically close the FileHandle when the stream is fully consumed.

Closes #25554 (partially - this addresses one of the remaining unimplemented methods)

Changes

  • Added readableWebStream() method to FileHandle class in ext/node/polyfills/internal/fs/handle.ts
  • Added tests for the new method in tests/unit_node/_fs/_fs_handle_test.ts

Test Plan

  • Added test for basic readableWebStream() functionality
  • Added test for autoClose: true option

AI Assistance Disclosure

I used Codex to review the changes, sanity-check the implementation against existing patterns, and help spot potential edge cases.

@CLAassistant
Copy link

CLAassistant commented Dec 31, 2025

CLA assistant check
All committers have signed the CLA.

- Fix autoClose: listen to 'end' event instead of 'close' (close doesn't fire with autoClose:false)
- Add one-time lock protection: throw ERR_INVALID_STATE when called twice
- Remove unused 'type' parameter from options
- Handle potential unhandled rejection in autoClose callback
- Improve tests: wait for close event and assert fd === -1 instead of setTimeout
- Use new ReadableStream({ type: 'bytes', pull, cancel }) directly
- Call FileHandle.read() in pull instead of using Readable.toWeb(createReadStream)
- Properly handle both EOF and cancel() for autoClose
- Add kRef/kUnref for proper resource management
- Add tests for cancel semantics with and without autoClose

This aligns with Node v22.17.0 implementation and avoids the side effects
of createReadStream's _destroy closing the fd directly.
@tt-a1i tt-a1i marked this pull request as ready for review December 31, 2025 16:16
tt-a1i added 5 commits January 1, 2026 00:27
- Save controller reference in start()
- On FileHandle close, call controller.error() which works even when locked
- Add test for closing FileHandle while reader is active
FileHandle extends EventEmitter but the TypeScript type definition
doesn't expose the once() method. Use type assertion to fix type
checking errors.
@bartlomieju bartlomieju requested a review from Tango992 January 7, 2026 12:16
Copy link
Member

@Tango992 Tango992 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, LGTM! I'd commit some changes before merging so it would pass the node compat test

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

FileHandle missing methods

3 participants