
Security News
The Hidden Blast Radius of the Axios Compromise
The Axios compromise shows how time-dependent dependency resolution makes exposure harder to detect and contain.
JavaScript implementation of Baba algorithm for robustly determining status changes of objects to be tracked
JavaScript implementation of the Baba algorithm for robustly determining status changes of objects to be tracked.
This is a JavaScript port of the original Python bbalg library.
npm install bbalgjs
or
yarn add bbalgjs
const { stateVerdict, createFixedQueue } = require('bbalgjs');
// Create fixed-size queues for tracking history
const longHistory = createFixedQueue(10); // Stores last 10 tracking results
const shortHistory = createFixedQueue(3); // Stores last 3 tracking results
// Simulate tracking over time
const trackingResults = [false, false, true, true, true, true, true, true, true, true];
trackingResults.forEach(detected => {
longHistory.push(detected);
shortHistory.push(detected);
// Always pass maxLength parameters (required)
const result = stateVerdict(
longHistory.toArray(),
shortHistory.toArray(),
10, // longMaxLength
3 // shortMaxLength
);
// Returns all false until queues are full
console.log(result);
});
import { stateVerdict, createFixedQueue } from 'bbalgjs';
// Same usage as CommonJS
<script src="https://unpkg.com/bbalgjs/dist/bbalgjs.umd.min.js"></script>
<script>
const { stateVerdict, createFixedQueue } = bbalgjs;
// Use the functions
const result = stateVerdict([true, true, false, true], [true, true], 4, 2);
console.log(result);
</script>
import { stateVerdict, createFixedQueue, StateVerdictResult, FixedQueue } from 'bbalgjs';
// TypeScript will infer types automatically
const longQueue: FixedQueue<boolean> = createFixedQueue(10);
const shortQueue: FixedQueue<boolean> = createFixedQueue(3);
// Process tracking data
const result: StateVerdictResult = stateVerdict(
longQueue.toArray(),
shortQueue.toArray(),
10, // longMaxLength
3 // shortMaxLength
);
The package works seamlessly in both Electron main and renderer processes:
Main Process:
// main.js
const { stateVerdict } = require('bbalgjs');
// Use in IPC handlers
ipcMain.handle('analyze-tracking', (event, longHistory, shortHistory, longMax, shortMax) => {
return stateVerdict(longHistory, shortHistory, longMax, shortMax);
});
Renderer Process:
// renderer.js
const { stateVerdict, createFixedQueue } = require('bbalgjs');
// or with ES modules
import { stateVerdict, createFixedQueue } from 'bbalgjs';
// Use directly in renderer
const result = stateVerdict(longHistory, shortHistory, 10, 3);
stateVerdict(longTrackingHistory, shortTrackingHistory, longMaxLength?, shortMaxLength?)Determines the state of an object based on tracking history.
Parameters:
longTrackingHistory (Array): N historical tracking results (older to newer)shortTrackingHistory (Array): M recent tracking results (older to newer)longMaxLength (number, optional): Expected maximum length of long tracking historyshortMaxLength (number, optional): Expected maximum length of short tracking historyNote: If you provide one maxLength parameter, you must provide both.
Returns: Object with three properties:
stateInProgress (boolean): Whether the state is currently in progress
stateStartJudgment (boolean): Whether the state has just started
stateEndJudgment (boolean): Whether the state has just ended
Throws: Error if:
Note:
maxLength parameters are provided, returns all false if history length is less than the specified maximum lengthcreateFixedQueue(maxLength)Creates a fixed-size queue (deque) that maintains a maximum length.
Parameters:
maxLength (number): Maximum number of items the queue can holdReturns: Object with methods:
push(item): Add an item to the queue (removes oldest if at capacity)toArray(): Get all items as an arraylength: Current number of items (getter)maxLength: Maximum capacity (getter)Throws: Error if maxLength is not a positive integer
const { stateVerdict, createFixedQueue } = require('bbalgjs');
class ObjectTracker {
constructor() {
this.longHistory = createFixedQueue(20);
this.shortHistory = createFixedQueue(5);
}
processFrame(objectDetected) {
this.longHistory.push(objectDetected);
this.shortHistory.push(objectDetected);
if (this.longHistory.length < 2 || this.shortHistory.length < 2) {
return null; // Not enough data yet
}
const verdict = stateVerdict(
this.longHistory.toArray(),
this.shortHistory.toArray(),
20, // longMaxLength
5 // shortMaxLength
);
if (verdict.stateStartJudgment) {
console.log('Object tracking started!');
} else if (verdict.stateEndJudgment) {
console.log('Object tracking ended!');
} else if (verdict.stateInProgress) {
console.log('Object is being tracked...');
}
return verdict;
}
}
// Usage
const tracker = new ObjectTracker();
// Simulate object detection over 30 frames
for (let i = 0; i < 30; i++) {
// Object appears after frame 10 and disappears after frame 20
const detected = i >= 10 && i < 20;
const result = tracker.processFrame(detected);
if (result) {
console.log(`Frame ${i}:`, result);
}
}
const { stateVerdict, createFixedQueue } = require('bbalgjs');
class MotionDetector {
constructor(sensitivity = { long: 15, short: 4 }) {
this.longHistory = createFixedQueue(sensitivity.long);
this.shortHistory = createFixedQueue(sensitivity.short);
}
analyzeMotion(motionValue, threshold = 0.1) {
// Convert motion value to boolean based on threshold
const motionDetected = motionValue > threshold;
this.longHistory.push(motionDetected);
this.shortHistory.push(motionDetected);
if (this.longHistory.length < 2 || this.shortHistory.length < 2) {
return { motion: 'initializing' };
}
const verdict = stateVerdict(
this.longHistory.toArray(),
this.shortHistory.toArray(),
20, // longMaxLength
5 // shortMaxLength
);
if (verdict.stateStartJudgment) {
return { motion: 'started', event: true };
} else if (verdict.stateEndJudgment) {
return { motion: 'ended', event: true };
} else if (verdict.stateInProgress) {
return { motion: 'ongoing', event: false };
} else {
return { motion: 'idle', event: false };
}
}
}
const { stateVerdict, createFixedQueue } = require('bbalgjs');
class StateMachine {
constructor() {
this.states = new Map();
}
addState(name, historyConfig = { long: 10, short: 3 }) {
this.states.set(name, {
longHistory: createFixedQueue(historyConfig.long),
shortHistory: createFixedQueue(historyConfig.short),
active: false
});
}
updateState(name, condition) {
const state = this.states.get(name);
if (!state) throw new Error(`State ${name} not found`);
state.longHistory.push(condition);
state.shortHistory.push(condition);
if (state.longHistory.length >= 2 && state.shortHistory.length >= 2) {
const verdict = stateVerdict(
state.longHistory.toArray(),
state.shortHistory.toArray()
);
const wasActive = state.active;
state.active = verdict.stateInProgress;
return {
state: name,
active: state.active,
changed: wasActive !== state.active,
verdict
};
}
return null;
}
}
// Usage
const machine = new StateMachine();
machine.addState('user_active', { long: 30, short: 5 });
machine.addState('high_cpu', { long: 20, short: 4 });
// Monitor states
setInterval(() => {
const userActive = machine.updateState('user_active', isUserActive());
const highCpu = machine.updateState('high_cpu', getCpuUsage() > 80);
if (userActive?.changed) {
console.log('User activity state changed:', userActive.active);
}
if (highCpu?.verdict.stateStartJudgment) {
console.log('High CPU usage detected!');
}
}, 1000);
The Baba algorithm uses two sliding windows of different sizes to make robust determinations about state changes:
The algorithm calculates three judgments:
This dual-window approach helps filter out noise and provides reliable state detection even in the presence of occasional false positives or negatives.
# Clone the repository
git clone https://github.com/PINTO0309/bbalgjs.git
cd bbalgjs
# Install dependencies
npm install
# Run tests
npm test
# Build the package
npm run build
MIT License - see LICENSE file for details.
Original Python implementation by PINTO0309.
FAQs
JavaScript implementation of Baba algorithm for robustly determining status changes of objects to be tracked
We found that bbalgjs 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
The Axios compromise shows how time-dependent dependency resolution makes exposure harder to detect and contain.

Research
A supply chain attack on Axios introduced a malicious dependency, plain-crypto-js@4.2.1, published minutes earlier and absent from the project’s GitHub releases.

Research
Malicious versions of the Telnyx Python SDK on PyPI delivered credential-stealing malware via a multi-stage supply chain attack.