npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

xstate-migrate

v0.0.5

Published

A migration library for persisted XState machines

Readme

xstate-migrate

xstate-migrate is a migration library for persisted XState machines, designed to facilitate state machine migrations when updating your XState configurations. This library generates and applies migration patches to ensure seamless transitions between different versions of your state machines.

Features

  • Generate migration patches for XState machines
  • Apply migration patches to persisted snapshots
  • Handle nested and parallel state machines
  • Support for adding and removing context properties

Installation

You can install xstate-migrate using npm:

npm install xstate-migrate

Usage

Generating Migrations

Use the generateMigrations function to generate a set of migration patches between the current and a new version of an XState machine.

import { createMachine, createActor } from 'xstate';
import { xstateMigrate } from 'xstate-migrate';

// Define your initial state machine
const machineV1 = createMachine({
  types: {
    input: {} as { initialData: string },
    context: {} as { data: string },
  },
  id: 'example',
  initial: 'idle',
  context: ({ input }) => ({ data: input.initialData }),
  states: {
    idle: { on: { NEXT: 'active' } },
    active: {},
  },
});

// Create an actor and get the initial snapshot
const actor = createActor(machineV1, {
  input: { initialData: 'Hello' },
}).start();
actor.send({ type: 'NEXT' });
const persistedSnapshot = actor.getSnapshot();

// Define your new state machine
const machineV2 = createMachine({
  types: {
    input: {} as { initialData: string },
    context: {} as { data: string; newData: number },
  },
  id: 'example',
  initial: 'idle',
  context: ({ input }) => ({ data: input.initialData, newData: 0 }),
  states: {
    idle: { on: { NEXT: 'active' } },
    active: {},
    newState: {},
  },
});

// Generate the migration patches
const migrations = xstateMigrate.generateMigrations(machineV2, persistedSnapshot, {
  initialData: 'Hello',
});

console.log(migrations);
/*
[
  { op: 'add', path: '/context/newData', value: 0 }
]
*/

Applying Migrations

Use the applyMigrations function to apply a set of migration patches to a persisted snapshot.

import { xstateMigrate } from 'xstate-migrate';

const persistedSnapshot = {
  context: { data: 'Hello' },
  value: 'active',
};

const migrations = [{ op: 'add', path: '/context/newData', value: 0 }];

const migratedSnapshot = xstateMigrate.applyMigrations(persistedSnapshot, migrations);

console.log(migratedSnapshot);
/*
{
  context: { data: 'Hello', newData: 0 },
  value: 'active'
}
*/

How Migrations Work

  • Migrations are generated as a list of JSON Patch operations that describe the changes needed to update a persisted snapshot to match a new state machine configuration.
  • The operations can include adding, removing, or replacing properties in the state machine's context or value.
  • The generateMigrations function compares the initial snapshot of the new state machine with the persisted snapshot and creates the necessary operations.
  • Existing context values are preserved during the migration process.
  • The applyMigrations function applies these operations to the persisted snapshot, updating it to reflect the new state machine configuration.

API

xstateMigrate.generateMigrations<TMachine extends AnyStateMachine>(machine: TMachine, persistedSnapshot: AnyMachineSnapshot, input?: InputFrom<TMachine>): Operation[]

Generates a list of migration patches for updating a persisted snapshot to match the new state machine configuration.

  • machine: The new version of the state machine.
  • persistedSnapshot: The persisted snapshot of the previous state machine.
  • input (optional): The input for the new state machine, matching its input type.

xstateMigrate.applyMigrations(persistedSnapshot: AnyMachineSnapshot, migrations: Operation[]): AnyMachineSnapshot

Applies a list of migration patches to a persisted snapshot.

  • persistedSnapshot: The persisted snapshot to be migrated.
  • migrations: The list of migration patches to apply.

Migration Format

The migrations generated by xstate-migrate follow the JSON Patch standard (RFC 6902). JSON Patch is a format for describing changes to a JSON document. It is a JSON document itself that contains an array of operations. Each operation is an object with the following properties:

  • op: The operation to perform. Can be one of add, remove, replace, move, copy, or test.
  • path: A JSON Pointer that indicates the location in the target document where the operation is to be performed.
  • value: The value to be used within the operations add, replace, and test.

Example migration:

[{ "op": "add", "path": "/context/newProp", "value": "default" }]

License

This project is licensed under the MIT License.