Skip to content

Implement comprehensive FileHandle API with EventEmitter, reference counting, and enhanced streaming#1112

Merged
streamich merged 5 commits intomasterfrom
copilot/fix-1089
Jul 25, 2025
Merged

Implement comprehensive FileHandle API with EventEmitter, reference counting, and enhanced streaming#1112
streamich merged 5 commits intomasterfrom
copilot/fix-1089

Conversation

Copy link
Copy Markdown
Contributor

Copilot AI commented Jul 25, 2025

This PR implements a comprehensive FileHandle API to align with Node.js behavior and complete the missing functionality outlined in the issue.

Key Changes

EventEmitter Integration

The FileHandle class now properly extends EventEmitter, enabling full event-based functionality:

  • Added support for on(), emit(), removeListener() and all EventEmitter methods
  • FileHandle now emits 'close' events when the handle is closed
  • Updated IFileHandle interface to extend EventEmitter

Resource Management

Implemented Node.js-style reference counting system:

  • Added internal reference counting (refs) to prevent premature closure
  • Multiple close() calls are handled gracefully with promise reuse
  • Added private ref() and unref() methods for proper resource lifecycle management

Modern API Support

  • FileHandle[Symbol.asyncDispose]: Added async disposal support for ES2023+ patterns
  • FileHandle#getAsyncId(): Added method returning the file descriptor for async resource tracking

Enhanced ReadableStream Implementation

Completely rewrote readableWebStream() with production-ready features:

  • Chunked reading: Large files are read in 16KB chunks instead of loading entirely into memory
  • BYOB support: Bring-your-own-buffer optimization for better performance
  • Proper error handling: Stream errors are properly propagated to the controller
  • Resource cleanup: Automatic reference management during streaming operations
  • Position tracking: Maintains file position across sequential reads

Example Usage

const fs = createFs();
fs.writeFileSync('/example.txt', 'Hello World');

const handle = await fs.promises.open('/example.txt', 'r');

// EventEmitter functionality
handle.on('close', () => {
  console.log('File handle closed');
});

// Enhanced streaming
const stream = handle.readableWebStream();
const reader = stream.getReader();
const { value, done } = await reader.read(); // Receives Uint8Array chunks

// Async disposal (ES2023+)
await handle[Symbol.asyncDispose]();

// Multiple close calls work safely
await handle.close();
await handle.close(); // No error, returns same promise

Testing

  • Added comprehensive test suite covering all new functionality
  • All existing tests continue to pass (1050 tests total)
  • New tests verify EventEmitter behavior, reference counting, streaming, and async disposal

The FileHandle implementation is now production-ready with full Node.js compatibility, proper resource management, and modern streaming capabilities.

Fixes #1089.


💬 Share your feedback on Copilot coding agent for the chance to win a $200 gift card! Click here to start the survey.

Copilot AI and others added 3 commits July 25, 2025 19:56
Co-authored-by: streamich <9773803+streamich@users.noreply.github.com>
…ng, and Symbol.asyncDispose

Co-authored-by: streamich <9773803+streamich@users.noreply.github.com>
…B support

Co-authored-by: streamich <9773803+streamich@users.noreply.github.com>
Copilot AI changed the title [WIP] Implement FileHandle API Implement comprehensive FileHandle API with EventEmitter, reference counting, and enhanced streaming Jul 25, 2025
Copilot AI requested a review from streamich July 25, 2025 20:06
Copy link
Copy Markdown
Owner

@streamich streamich left a comment

Choose a reason for hiding this comment

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

LGTM

@streamich streamich marked this pull request as ready for review July 25, 2025 22:18
@streamich streamich merged commit 4b39403 into master Jul 25, 2025
12 checks passed
@streamich streamich deleted the copilot/fix-1089 branch July 25, 2025 22:19
@github-actions
Copy link
Copy Markdown
Contributor

🎉 This PR is included in version 4.20.0 🎉

The release is available on:

Your semantic-release bot 📦🚀

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

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Implement FileHandle API

2 participants