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

polyglot-book-js

v1.0.0

Published

Production-ready JavaScript library for reading Polyglot opening book files (.bin) with FEN-based position lookup

Readme

polyglot-book-js

A production-ready JavaScript/TypeScript library for reading Polyglot opening book files (.bin). Uses FEN strings as input and returns matching opening moves with weights.

npm version License: MIT

Features

Spec-compliant - Implements the official Polyglot Zobrist hashing algorithm
Zero dependencies - No external chess libraries required
TypeScript - Full type definitions included
Cross-platform - Works in Node.js and browsers
Efficient - Binary search for fast lookups
Well-tested - Verified against official Polyglot test data

Installation

npm install polyglot-book-js

Quick Start

Node.js

import fs from 'fs';
import { getBookMoves } from 'polyglot-book-js';

// Read a Polyglot book file
const bookData = fs.readFileSync('book.bin');

// Get moves for the starting position
const startFEN = 'rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1';
const moves = getBookMoves(startFEN, bookData);

console.log(moves);
// [
//   { uci: 'e2e4', weight: 5000, learn: 0, from: 12, to: 28 },
//   { uci: 'd2d4', weight: 3000, learn: 0, from: 11, to: 27 },
//   ...
// ]

Browser

import { getBookMoves } from 'polyglot-book-js';

// Fetch a book file
const response = await fetch('book.bin');
const bookData = await response.arrayBuffer();

// Get moves
const fen = 'rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1';
const moves = getBookMoves(fen, bookData);

console.log(moves); // Sorted by weight (descending)

API Reference

getBookMoves(fen, bookData)

Get all book moves for a given position.

Parameters:

  • fen (string) - FEN string representing the position
  • bookData (ArrayBuffer | Uint8Array) - Polyglot book file data

Returns: BookMove[] - Array of moves sorted by weight (descending)

BookMove interface:

interface BookMove {
  uci: string;           // UCI notation (e.g., "e2e4", "e7e8q")
  weight: number;        // Move weight from the book
  learn: number;         // Learn value from the book
  from: number;          // From square (0-63)
  to: number;            // To square (0-63)
  promotion?: string;    // Promotion piece if applicable ('n', 'b', 'r', 'q')
}

computePolyglotHash(fen)

Compute the Polyglot Zobrist hash for a position.

Parameters:

  • fen (string) - FEN string

Returns: bigint - 64-bit Zobrist hash

Example:

import { computePolyglotHash } from 'polyglot-book-js';

const hash = computePolyglotHash('rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1');
console.log(hash.toString(16)); // "463b96181691fc9c"

parseFEN(fen)

Parse a FEN string into structured data.

Parameters:

  • fen (string) - FEN string

Returns: FENData - Parsed FEN components

Example:

import { parseFEN } from 'polyglot-book-js';

const data = parseFEN('rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1');
console.log(data.sideToMove); // 'w'
console.log(data.castling.whiteKingside); // true

Polyglot Book Format

This library implements the official Polyglot opening book specification:

  • File format: Binary .bin files
  • Entry size: 16 bytes per entry
    • 8 bytes: Zobrist hash key (big-endian)
    • 2 bytes: Encoded move (big-endian)
    • 2 bytes: Weight (big-endian)
    • 4 bytes: Learn value (big-endian)
  • Sorting: Entries are sorted by hash key
  • Lookup: Binary search for efficient retrieval

Zobrist Hashing

The library uses the official Polyglot Zobrist hashing algorithm:

  • Random table: 781 pre-generated 64-bit values
  • Piece hashing: XOR values based on piece type and square
  • Castling: Individual flags for K, Q, k, q
  • En passant: Hashed only if a legal capture exists
  • Side to move: Hashed when it's White's turn

TypeScript Support

Full TypeScript definitions are included:

import { getBookMoves, BookMove, FENData } from 'polyglot-book-js';

const moves: BookMove[] = getBookMoves(fen, bookData);
const topMove: BookMove = moves[0];
console.log(topMove.uci); // Type-safe!

Error Handling

The library throws descriptive errors for invalid input:

try {
  const moves = getBookMoves('invalid fen', bookData);
} catch (error) {
  console.error(error.message); // "FEN must have at least 4 parts..."
}

Performance

  • Binary search: O(log n) lookup time
  • Memory efficient: Doesn't load entire book into memory
  • BigInt operations: Proper 64-bit hash handling

Where to Get Polyglot Books

Popular Polyglot opening books:

  • Performance.bin - Balanced opening repertoire
  • Human.bin - Human-like opening choices
  • Computer.bin - Engine-optimized openings

You can create custom books using tools like:

  • polyglot make-book command
  • Chess engine analysis tools

License

MIT © 2026

Contributing

Contributions are welcome! Please feel free to submit issues or pull requests.

Related Projects

Acknowledgments

  • Zobrist random table from the official Polyglot specification
  • Test data verified against python-chess implementation