Skip to content

feat: replace peek-stream with native node:stream#394

Open
hardik-kaji wants to merge 2 commits intofastify:mainfrom
hardik-kaji:feat/remove-peek-stream
Open

feat: replace peek-stream with native node:stream#394
hardik-kaji wants to merge 2 commits intofastify:mainfrom
hardik-kaji:feat/remove-peek-stream

Conversation

@hardik-kaji
Copy link
Copy Markdown

@hardik-kaji hardik-kaji commented Apr 30, 2026

Why

The peek-stream package depends on through2 and xtend — all of which are
legacy userland stream utilities. In particular, xtend is no longer maintained.
Removing peek-stream reduces the dependency tree, avoids maintenance/security risks
from unmaintained packages, and aligns with the ongoing effort (#386) to replace
userland stream helpers with native node:stream APIs.

Summary

  • Removes the peek-stream dependency by replacing it with a native createPeekStream
    implementation using node:stream Duplex
  • Fixes a bug where onpeek received the entire buffered data instead of only the
    first maxBuffer bytes when a single write exceeded maxBuffer
  • Moves createPeekStream to lib/utils.js with JSDoc documentation and comprehensive
    unit tests

Changes

  • index.js — Remove inline createPeekStream, import it from lib/utils.js, remove
    unused Duplex import
  • lib/utils.js — Add createPeekStream with proper peek slicing logic and JSDoc
  • test/utils.test.js — Add 6 unit tests covering peek correctness, chunked input,
    short streams, error propagation, empty streams, and transform destination

Checklist

Remove the peek-stream dependency which pulls in through2 and xtend
(EOL since 2015). Replace with a lightweight createPeekStream helper
using node:stream.Duplex — zero new dependencies.

This eliminates 3 transitive packages: peek-stream, through2, xtend.
@hardik-kaji

This comment was marked as spam.

@fritzfelix
Copy link
Copy Markdown

Thank you for this. Unfortunately those EOL packages lead to quite some headaches within corporate environments, thus it's much easier to use fastify in those environments if it's clean.

Copy link
Copy Markdown
Member

@mcollina mcollina left a comment

Choose a reason for hiding this comment

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

This seem expensive perf-wise. Maybe we can drop peek-stream and have a better interface for this?

Comment thread lib/utils.js Outdated
Comment thread lib/utils.js
Refactor createPeekStream into createLazyTransform:
- Use Transform instead of Duplex for a simpler, idiomatic interface
- Synchronous `choose(data)` replaces callback-based `swap(err, stream)`
- Buffer chunks in an array, concat only once when threshold is reached
- Default maxBuffer to 10 (the only value used in practice)
- Rename to createLazyTransform to better reflect its purpose
@hardik-kaji hardik-kaji force-pushed the feat/remove-peek-stream branch from b179ac3 to c82c2f3 Compare May 4, 2026 12:03
@hardik-kaji
Copy link
Copy Markdown
Author

hardik-kaji commented May 4, 2026

This seem expensive perf-wise. Maybe we can drop peek-stream and have a better interface for this?

@mcollina Done! Refactored createPeekStream into createLazyTransform with a simpler, more performant interface:

  • Transform instead of Duplex — uses idiomatic transform/flush instead of manual write/final/read with
    event listener wiring
  • Synchronous choose(data) instead of callback-based swap(err, stream) — the caller just returns the
    desired stream directly, no callback overhead
  • Array-based buffering — chunks are collected in an array and Buffer.concat is called only once when the
    threshold is reached (instead of on every chunk)
  • Default maxBuffer = 10 — since both callers always pass 10, it's now the default

The callers are now much cleaner:

  function zipStream (deflate, encoding) {
    return createLazyTransform(function (data) {
      switch (isCompressed(data)) {
        case 1: return new Minipass()
        case 2: return new Minipass()
      }
      return deflate[encoding]()
    })
  }

@hardik-kaji hardik-kaji requested a review from mcollina May 4, 2026 12:06
@hardik-kaji hardik-kaji changed the title feat: replace peek-stream with native node:stream Duplex feat: replace peek-stream with native node:stream- May 4, 2026
@hardik-kaji hardik-kaji changed the title feat: replace peek-stream with native node:stream- feat: replace peek-stream with native node:stream May 4, 2026
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.

3 participants