Compression Streams API
Baseline
Widely available
*
This feature is well established and works across many devices and browser versions. It’s been available across browsers since May 2023.
* Some parts of this feature may have varying levels of support.
Note: This feature is available in Web Workers.
Compression Streams API cung cấp một API JavaScript để nén và giải nén các luồng dữ liệu bằng định dạng gzip hoặc deflate.
Khả năng nén tích hợp sẵn có nghĩa là các ứng dụng JavaScript sẽ không cần phải kèm theo một thư viện nén, giúp kích thước tải xuống của ứng dụng nhỏ hơn.
Response của Fetch API có thể được dùng để chuyển đổi các luồng thành:
ArrayBufferBlobUint8ArrayString- JSON
Giao diện
CompressionStream-
Nén một luồng dữ liệu.
DecompressionStream-
Giải nén một luồng dữ liệu.
Ví dụ
Trong ví dụ này, một luồng được nén bằng phương thức nén gzip.
const compressedReadableStream = inputReadableStream.pipeThrough(
new CompressionStream("gzip"),
);
Trong ví dụ sau, một hàm giải nén một blob bằng gzip.
async function DecompressBlob(blob) {
const ds = new DecompressionStream("gzip");
const decompressedStream = blob.stream().pipeThrough(ds);
return await new Response(decompressedStream).blob();
}
Thông số kỹ thuật
| Specification |
|---|
| Compression> # compression-stream> |