Swift example
The iOS UasmUniZstd.xcframework contains two independent entry points in the same
framework binary:
binding.ccregisters the existing Node-API module.swift_binding.ccexposes a C ABI that does not include or call Node-API.
Add UasmUniZstd.xcframework to the application, then add utsdk/app-ios/UniZstd.swift to the
Swift target. The wrapper imports the framework's UasmUniZstd Clang module and maps
the C result to Swift Data and Error values.
let compressed = try UniZstd.compress("hello from Swift")
let restored = try UniZstd.decompress(compressed)
let text = String(decoding: restored, as: UTF8.self)
let binary = try UniZstd.compress(Data([1, 2, 3]), level: 10)
let parallel = try UniZstd.compress(largeInput, level: 10, workers: 2)
let decoded = try UniZstd.decompress(
binary,
maximumOutputSize: 16 * 1024 * 1024
)
Incremental compression and decompression use ZstdCompressor and
ZstdDecompressor. Every update returns only the output produced by that
input chunk; append or write it before processing the next chunk, then append
the output from finish():
let compressor = try ZstdCompressor(level: 10, workers: 2)
var compressed = Data()
compressed.append(try compressor.update(firstChunk))
compressed.append(try compressor.update(secondChunk))
compressed.append(try compressor.finish())
let decompressor = try ZstdDecompressor(maximumOutputSize: 16 * 1024 * 1024)
var restored = Data()
restored.append(try decompressor.update(compressed.prefix(7)))
restored.append(try decompressor.update(compressed.dropFirst(7)))
restored.append(try decompressor.finish())
finish() closes the instance and checks that decompression ended on a full
frame. Dropping an instance before finish() safely releases its native stream.
workers defaults to 0 (single-threaded); positive values enable that many
zstd compression workers.
The Wasm build pre-creates four workers by default. Set PTHREAD_POOL_SIZE to
change that limit, for example make BUILD_TYPE=Release PTHREAD_POOL_SIZE=8 build.
The C bridge owns its result memory. UniZstd.swift copies output into Data and
always destroys the native result before returning. Swift calls do not need an
initialized Node-API runtime, although the same framework still retains the
Node-API symbols used by the JavaScript host.