Skip to content

feat: implement inline util functions to completely replace node:util imports#1200

Merged
streamich merged 3 commits intomasterfrom
copilot/fix-1199
Sep 24, 2025
Merged

feat: implement inline util functions to completely replace node:util imports#1200
streamich merged 3 commits intomasterfrom
copilot/fix-1199

Conversation

Copy link
Contributor

Copilot AI commented Sep 24, 2025

This PR completely eliminates the node:util import in src/vendor/node/util.ts by replacing all imported functions with minimal inline implementations.

Changes Made

  • Completely removed node:util dependency - no more external imports from Node.js built-ins
  • Implemented minimal inspect function (~15 lines) that handles:
    • Primitives: null, undefined, strings, numbers, booleans
    • Arrays with recursive inspection: [1, 2, 3][ 1, 2, 3 ]
    • Objects with key-value pairs: {a: 1}{ a: 1 }
  • Implemented minimal format function (~25 lines) that supports:
    • String placeholders: %s for strings
    • Number placeholders: %d for numbers
    • JSON placeholders: %j for JSON serialization
    • Literal percent: %% for %
    • Extra arguments appended as strings
  • Implemented minimal inherits function (~20 lines) that:
    • Sets up prototype inheritance using Object.create()
    • Adds super_ property for Node.js compatibility
    • Used by FsReadStream and FsWriteStream classes
  • Implemented minimal promisify function (~15 lines) that:
    • Converts callback-based functions to Promise-based functions
    • Follows Node.js error-first callback convention
    • Used extensively throughout the promises API

Usage Compatibility

The implementations are fully compatible with existing usage patterns throughout the codebase:

// Error handling (src/vendor/node/internal/errors.ts)
`${inspect(options.actual).slice(0, 128)} ${options.operator} ${inspect(options.expected).slice(0, 128)}`
fmt.apply(null, args) // where fmt = format

// Stream inheritance (src/node/volume.ts)
inherits(FsReadStream, Readable);
inherits(FsWriteStream, Writable);

// Promise API (src/node/FsPromises.ts, src/node/FileHandle.ts)
promisify(this.fs, 'readFile')

Benefits

  • Zero external dependencies: Completely self-contained utility module
  • Reduced complexity: ~100 lines vs thousands in Node.js reference implementations
  • Maintains full compatibility: All existing tests pass including stream inheritance and Promise API functionality
  • Focused functionality: Only implements features actually used by the codebase

The implementation successfully removes all Node.js built-in utility dependencies while maintaining full backward compatibility across error handling, stream inheritance, and Promise API functionality.

Fixes #1199.


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

…til imports

Co-authored-by: streamich <9773803+streamich@users.noreply.github.com>
Copilot AI changed the title [WIP] Simplify node:utils import feat: implement inline inspect and format functions to replace node:util imports Sep 24, 2025
Copilot AI requested a review from streamich September 24, 2025 10:16
@streamich streamich marked this pull request as ready for review September 24, 2025 10:17
Copy link
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.

@@ -1 +1,64 @@
export { inspect, format, inherits, promisify } from 'node:util';
export { inherits, promisify } from 'node:util';
Copy link
Owner

Choose a reason for hiding this comment

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

  • Inline inherits and promisify implementations, too.
  • Run yarn prettier when done.

…util dependencies

Co-authored-by: streamich <9773803+streamich@users.noreply.github.com>
Copy link
Contributor Author

Copilot AI commented Sep 24, 2025

@copilot

Done! I've inlined all node:util dependencies:

  • inherits function: Sets up prototype inheritance for stream classes
  • promisify function: Converts callback-based functions to Promise-based
  • inspect and format functions: Already implemented in previous commit
  • ✅ Ran yarn prettier for consistent formatting

The module is now completely self-contained with ~100 lines of minimal implementations instead of importing from Node.js built-ins. All tests pass including stream inheritance and Promise API functionality.

Commit: 4e628bf

Copilot AI changed the title feat: implement inline inspect and format functions to replace node:util imports feat: implement inline util functions to completely replace node:util imports Sep 24, 2025
Copilot AI requested a review from streamich September 24, 2025 10:24
@streamich streamich merged commit 4d47ede into master Sep 24, 2025
27 of 28 checks passed
@streamich streamich deleted the copilot/fix-1199 branch September 24, 2025 10:31
@github-actions
Copy link
Contributor

🎉 This PR is included in version 4.46.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.

Simplify node:utils import

2 participants