Kitura 压缩中间件,用于压缩发送回客户端的主体数据。支持 deflate
和 gzip
压缩方法。使用 zlib。
最新版本的 Kitura-Compression 需要 Swift 4.0 或更高版本。您可以通过点击此链接下载此版本的 Swift 二进制文件。不保证与其他 Swift 版本的兼容性。
将 Kitura-Compression
包添加到应用程序 Package.swift
文件中的依赖项中。将 "x.x.x"
替换为最新的 Kitura-Compression
版本。
.package(url: "https://github.com/Kitura/Kitura-Compression.git", from: "x.x.x")
将 KituraCompression
添加到你的目标依赖项中
.target(name: "example", dependencies: ["KituraCompression"]),
import KituraCompression
为了使用压缩中间件,请创建 Compression
的实例,并将其连接到所需的路径
import KituraCompression
router.all(middleware: Compression())
你可以使用可选参数配置 Compression
public init (threshold: Int = 1024, chunkSize: Int = 16384, compressionLevel: CompressionLevel = CompressionLevel.defaultCompression, compressionStrategy: CompressionStrategy = CompressionStrategy.defaultStrategy, memoryLevel: Int32 = 8)
其中
threshold 是响应主体大小的字节阈值,超过此阈值才考虑对响应进行压缩,默认为 1024。
chunkSize 是内部输出 slab 缓冲区的大小(以字节为单位),默认为 16384。
compressionLevel 是要应用的 zlib 压缩级别。支持的值包括
.noCompression、.bestSpeed、.bestCompression、.defaultCompression
compressionStrategy 用于调整压缩算法。可能的值包括
.defaultStrategy、.filtered、.huffmanOnly、.rle、.fixed
memoryLevel 指定应为内部压缩状态分配多少内存。默认值为 8。
有关压缩参数的更多信息,请参阅 zlib 手册。
有关更多信息,请访问我们的 API 参考。
我们喜欢讨论服务器端 Swift 和 Kitura。加入我们的 Slack 与团队会面!
此库在 Apache 2.0 许可下获得许可。完整许可文本可在 LICENSE 中找到。