
Security News
Attackers Are Hunting High-Impact Node.js Maintainers in a Coordinated Social Engineering Campaign
Multiple high-impact npm maintainers confirm they have been targeted in the same social engineering campaign that compromised Axios.
xstate-migrate
Advanced tools
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.
You can install xstate-migrate using npm:
npm install xstate-migrate
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 }
]
*/
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'
}
*/
generateMigrations function compares the initial snapshot of the new state machine with the persisted snapshot and creates the necessary operations.applyMigrations function applies these operations to the persisted snapshot, updating it to reflect the new state machine configuration.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[]): AnyMachineSnapshotApplies a list of migration patches to a persisted snapshot.
persistedSnapshot: The persisted snapshot to be migrated.migrations: The list of migration patches to apply.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" }]
This project is licensed under the MIT License.
FAQs
A migration library for persisted XState machines
We found that xstate-migrate demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Security News
Multiple high-impact npm maintainers confirm they have been targeted in the same social engineering campaign that compromised Axios.

Security News
Axios compromise traced to social engineering, showing how attacks on maintainers can bypass controls and expose the broader software supply chain.

Security News
Node.js has paused its bug bounty program after funding ended, removing payouts for vulnerability reports but keeping its security process unchanged.