Vize

use-compression-stream

Compress data with the Compression Streams API.

Package @vizejs/composable/use-compression-stream
Own the source vize lib pull composable:use-compression-stream
Runtime exports compress, decompress, decompressText, useCompressionStream
Gzip budget 2304 B

Usage

import { compress, decompress, decompressText, useCompressionStream } from "@vizejs/composable/use-compression-stream";

Runtime contract

Utility Category Stability SSR Hydration Cleanup Targets Host globals Uses
compress system experimental deterministic-fallback not-applicable none web, desktop Blob, ReadableStream, TextDecoder, TextEncoder, window —
decompress system experimental deterministic-fallback not-applicable none web, desktop Blob, ReadableStream, TextDecoder, TextEncoder, window —
decompressText system experimental deterministic-fallback not-applicable none web, desktop Blob, ReadableStream, TextDecoder, TextEncoder, window —
useCompressionStream system experimental deterministic-fallback caller-managed none web, desktop Blob, ReadableStream, TextDecoder, TextEncoder, window compress, decompress, decompressText

API

compress

Compress data with the Compression Streams API. Strings are UTF-8 encoded first. Rejects with a tagged TypeError for an unknown format or when no CompressionStream is available (always on the server unless one is injected).

function compress( data: CompressionInput, format: CompressionFormatName = "gzip", options: CompressOptions = {}, ): Promise<Uint8Array<ArrayBuffer>>
const gzipped = await compress(JSON.stringify(state), "gzip");

decompress

Decompress data with the Compression Streams API. Rejects with a tagged TypeError for an unknown format or when no DecompressionStream is available, and with the stream's error for corrupt input.

function decompress( data: CompressionInput, format: CompressionFormatName = "gzip", options: DecompressOptions = {}, ): Promise<Uint8Array<ArrayBuffer>>
const bytes = await decompress(await response.blob(), "gzip");

decompressText

Decompress data and decode the result as UTF-8 text. Rejects exactly like decompress.

async function decompressText( data: CompressionInput, format: CompressionFormatName = "gzip", options: DecompressOptions = {}, ): Promise<string>
const json = JSON.parse(await decompressText(bytes, "deflate"));

useCompressionStream

Reactive wrapper around compress, decompress and decompressText. Tracks whether an action is in flight and records the last failure in error; actions still reject so callers can handle failures inline. Nothing needs cleanup. Server rendering: supported is false and actions reject with the tagged unsupported error unless constructors are injected. Inside a component supported turns true only after mounting, so hydration renders this server state first.

function useCompressionStream( options: UseCompressionStreamOptions = {}, ): CompressionStreamControls
const { compress, decompressText, pending } = useCompressionStream({ format: "deflate" });
const packed = await compress(draft.value);

Types

CompressionTransformLike

Transform stream created by a (de)compression constructor.

Member Type Description
readable ReadableStream<Uint8Array> Transformed output.
writable WritableStream<BufferSource> Raw input.

CompressOptions

Options for compress.

Member Type Description
CompressionStream? CompressionStreamConstructor | null | undefined Compression stream constructor.

DecompressOptions

Options for decompress and decompressText.

Member Type Description
DecompressionStream? CompressionStreamConstructor | null | undefined Decompression stream constructor.

UseCompressionStreamOptions

Options for useCompressionStream.

Member Type Description
CompressionStream? MaybeRef<CompressionStreamConstructor | null | undefined> Compression stream constructor. A ref (not a getter) because the host is a constructor function.
DecompressionStream? MaybeRef<CompressionStreamConstructor | null | undefined> Decompression stream constructor.
format? CompressionFormatName Format used when an action is called without one.

CompressionStreamControls

Reactive state and actions returned by useCompressionStream.

Member Type Description
supported ComputedRef<boolean> Whether both CompressionStream and DecompressionStream are available.
pending Readonly<Ref<boolean>> Whether an action is in flight.
error Readonly<ShallowRef<unknown>> Most recent failure, cleared by the next successful action.
compress ( data: CompressionInput, format?: CompressionFormatName, ) => Promise<Uint8Array<ArrayBuffer>> Compress data; rejects like compress.
decompress ( data: CompressionInput, format?: CompressionFormatName, ) => Promise<Uint8Array<ArrayBuffer>> Decompress data; rejects like decompress.
decompressText ( data: CompressionInput, format?: CompressionFormatName, ) => Promise<string> Decompress data and decode it as UTF-8.