Versioned state

luastra/state

Encodes small deterministic snapshots with explicit schema versions, decodes current snapshots, and migrates supported older versions through ordered functions. It separates serialization compatibility from the host used to persist the resulting string.

Keep snapshot fields bounded and free of secrets, store the encoded string through Host.storageSet, and check every DecodeResult or MigrationResult before restoring state.

Changing stored meaning requires a new version and tested migrations rather than an unchecked cast.

Complete example

local State = require("luastra/state")

local snapshot = State.encode(1, {
    route = "game",
    score = tostring(score),
})

local restored = State.decode(snapshot, 1)
if restored.success then
    score = tonumber(restored.fields.score) or 0
end

Detail pages

State.FieldsState.DecodeErrorState.DecodeSuccessState.DecodeFailureState.DecodeResultState.MigrationErrorState.MigrationSuccessState.MigrationFailureState.MigrationResultState.MigrationState.encodeState.decodeState.migrate