...

/

Solution: Compress Files with Switchable Encoders

Solution: Compress Files with Switchable Encoders

Implement asynchronous encoder strategies and delegate file compression through a configurable compressor context.

We'll cover the following...

Solution explanation

  • Lines 2–22: We define three encoder strategies: GzipEncoderBrotliEncoder, and NoCompression.

    • Each implements an asynchronous .compress() method that returns a Promise.

    • GzipEncoder simulates fast compression with a short delay.

    • BrotliEncoder takes longer, reflecting higher compression cost.

    • NoCompression resolves immediately, returning the data unchanged.

    • This demonstrates asynchronous strategies that share the same interface.

  • Lines 25–38: The Compressor class is the context.

    • It stores a reference to the current encoder strategy. ...