
Security News
Feross on the 10 Minutes or Less Podcast: Nobody Reads the Code
Socket CEO Feross Aboukhadijeh joins 10 Minutes or Less, a podcast by Ali Rohde, to discuss the recent surge in open source supply chain attacks.
A compact finite state machine for JavaScript and TypeScript, with async callback support and zero runtime dependencies.
falseOnBefore, OnLeave, OnAfter, OnEnter — sync or asyncOnBefore callbacks abort a transition by returning falseStateSet calls nested inside a callback return false without side effectsnpm / bundler:
npm install jcfsm
Browser (CDN):
<script type="module">
import jCFSM from 'https://unpkg.com/jcfsm@2.0.0/jcfsm.min.js';
const fsm = new jCFSM( 'idle' );
fsm.StateAdd( 'running' );
fsm.TransitionAdd( 'idle', 'running' );
await fsm.StateSet( 'running' );
console.log( fsm.StateGet() ); // 'running'
</script>
import jCFSM from 'jcfsm';
const fsm = new jCFSM( 'idle' );
fsm.StateAdd( 'running' );
fsm.StateAdd( 'stopped' );
fsm.TransitionAdd( 'idle', 'running' );
fsm.TransitionAdd( 'running', 'stopped' );
fsm.StateOnEnterAdd( 'running', ( current, prev ) => {
console.log( `Entered ${current} from ${prev}` );
} );
fsm.TransitionOnBeforeAdd( 'running', 'stopped', async () => {
const allowed = await checkIfStopIsAllowed();
return allowed;
} );
await fsm.StateSet( 'running' ); // true
await fsm.StateSet( 'idle' ); // false — no transition defined
await fsm.StateSet( 'stopped' ); // true or false — depends on the guard
When StateSet succeeds, callbacks fire in this order:
OnBefore → OnLeave → OnAfter → OnEnter
If any OnBefore callback returns false, the transition aborts immediately. OnLeave, OnAfter, and OnEnter do not fire.
new jCFSM( initialState: string )
Creates a new FSM. The initial state is registered automatically.
| Method | Returns | Description |
|---|---|---|
StateAdd( state ) | boolean | Registers a new state. Returns false if the state already exists. |
StateDel( state ) | boolean | Removes a state and all its associated transitions. Returns false if the state does not exist. |
StateGet() | string | Returns the current state. |
StateSet( state ) | Promise<boolean> | Triggers a transition to state. Returns false if the transition is not defined, if a guard aborts it, or if another transition is already in progress. |
These callbacks fire whenever the machine enters or leaves a specific state, regardless of which transition triggered the change.
Signatures:
type FunctionOnEnter = ( currentState: string, prevState: string ) => void | Promise<void>;
type FunctionOnLeave = ( currentState: string, nextState: string ) => void | Promise<void>;
| Method | Description |
|---|---|
StateOnEnterAdd( state, func ) | Registers a callback that fires after entering state. Returns false if the state does not exist or the callback is already registered. |
StateOnEnterDel( state, func ) | Removes an enter callback. Returns false if the callback is not found. |
StateOnLeaveAdd( state, func ) | Registers a callback that fires before leaving state. Returns false if the state does not exist or the callback is already registered. |
StateOnLeaveDel( state, func ) | Removes a leave callback. Returns false if the callback is not found. |
Transitions define which state changes are allowed. StateSet fails unless the corresponding transition is registered.
| Method | Returns | Description |
|---|---|---|
TransitionAdd( from, to ) | boolean | Registers the transition from from to to. Returns false if either state does not exist or the transition already exists. |
TransitionDel( from, to ) | boolean | Removes the transition. Returns false if the transition does not exist. |
These callbacks fire only for a specific from → to pair.
Signatures:
type FunctionOnTransitionBefore = () => boolean | Promise<boolean>;
type FunctionOnTransitionAfter = () => void | Promise<void>;
| Method | Description |
|---|---|
TransitionOnBeforeAdd( from, to, func ) | Registers a guard that fires before the transition. Return false to abort. Returns false if the transition does not exist. |
TransitionOnBeforeDel( from, to, func ) | Removes a before callback. Returns false if the callback is not found. |
TransitionOnAfterAdd( from, to, func ) | Registers a callback that fires after the transition completes. Returns false if the transition does not exist. |
TransitionOnAfterDel( from, to, func ) | Removes an after callback. Returns false if the callback is not found. |
See LICENSE.md.
FAQs
Compact implementation of a finite state machine
The npm package jcfsm receives a total of 0 weekly downloads. As such, jcfsm popularity was classified as not popular.
We found that jcfsm 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
Socket CEO Feross Aboukhadijeh joins 10 Minutes or Less, a podcast by Ali Rohde, to discuss the recent surge in open source supply chain attacks.

Research
/Security News
Campaign of 108 extensions harvests identities, steals sessions, and adds backdoors to browsers, all tied to the same C2 infrastructure.

Security News
OpenAI rotated macOS signing certificates after a malicious Axios package reached its CI pipeline in a broader software supply chain attack.