• Stars
    star
    237
  • Rank 163,535 (Top 4 %)
  • Language
    C++
  • License
    GNU General Publi...
  • Created over 5 years ago
  • Updated about 1 year ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

WebAssembly port of the strong chess engine Stockfish

stockfish.wasm

WebAssembly port of the strong chess engine Stockfish. See it in action on https://lichess.org/analysis.

npm version CI Passively maintained

Superceded by Stockfish with NNUE

This port is branched from SF_classical. See https://github.com/hi-ogawa/Stockfish for a WebAssembly port with NNUE support.

Requirements

Uses the latest WebAssembly threading proposal. Requires these HTTP headers on the top level response:

Cross-Origin-Embedder-Policy: require-corp
Cross-Origin-Opener-Policy: same-origin

And the following header on the included files:

Cross-Origin-Embedder-Policy: require-corp

If headers are not configured correctly on the top level response, wasmThreadsSupported() as defined below will return false. If headers are not configured correctly on the included files, something like pthread sent an error! undefined:undefined: undefined may be logged to the console.

Chromium based (desktop only)

  • Since Chromium 79: Full support.
  • Chromium 74: Supports treading, but cannot allocate additional memory. The default allocation suffices for up to 2 threads and 16 MB hash.
  • Chromium 70 to 73: Needs flag chrome://flags/#enable-webassembly-threads or Origin Trial.

Firefox

  • Since Firefox 79: Full support.
  • Firefox 72 to 78: Structured cloning can no longer be enabled with flags, except on nightlies.
  • Firefox 71: Requires javascript.options.shared_memory and dom.postMessage.sharedArrayBuffer.withCOOP_COEP to be enabled in about:flags.
  • Firefox 68 to 70: Requires javascript.options.shared_memory to be enabled in about:flags

Other browsers

No support.

See stockfish.js for a more portable but single-threaded version.

Feature detection

function wasmThreadsSupported() {
  // WebAssembly 1.0
  const source = Uint8Array.of(0x0, 0x61, 0x73, 0x6d, 0x01, 0x00, 0x00, 0x00);
  if (
    typeof WebAssembly !== "object" ||
    typeof WebAssembly.validate !== "function"
  )
    return false;
  if (!WebAssembly.validate(source)) return false;

  // SharedArrayBuffer
  if (typeof SharedArrayBuffer !== "function") return false;

  // Atomics
  if (typeof Atomics !== "object") return false;

  // Shared memory
  const mem = new WebAssembly.Memory({ shared: true, initial: 8, maximum: 16 });
  if (!(mem.buffer instanceof SharedArrayBuffer)) return false;

  // Structured cloning
  try {
    // You have to make sure nobody cares about these messages!
    window.postMessage(mem, "*");
  } catch (e) {
    return false;
  }

  // Growable shared memory (optional)
  try {
    mem.grow(8);
  } catch (e) {
    return false;
  }

  return true;
}

Current limitations

  • Hashtable: 1024 MB. You may want to check navigator.deviceMemory before allocating.
  • Threads: 32. You may want to check navigator.hardwareConcurrency. May be capped lower (e.g., dom.workers.maxPerDomain in Firefox).
  • Can hang when UCI protocol is misused. (Do not send invalid commands or positions. While the engine is searching, do not change options or start additional searches).
  • No NNUE support.
  • No Syzygy tablebase support.

Building

Assuming em++ (^2.0.13) is available:

npm run-script prepare

Usage

Requires stockfish.js, stockfish.wasm and stockfish.worker.js (total size ~400K, ~150K gzipped) to be served from the same directory.

<script src="stockfish.js"></script>
<script>
  Stockfish().then((sf) => {
    sf.addMessageListener((line) => {
      console.log(line);
    });

    sf.postMessage("uci");
  });
</script>

Or from recent node (v14.4.0 tested) with flags --experimental-wasm-threads --experimental-wasm-bulk-memory:

const Stockfish = require("stockfish.wasm"); // the module, not the file

Stockfish().then((sf) => {
  // ...
});

License

Thanks to the Stockfish team for sharing the engine under the GPL3.

More Repositories

1

lila

β™ž lichess.org: the forever free, adless and open source chess server β™ž
Scala
14,433
star
2

lichobile

lichess.org mobile application
TypeScript
1,969
star
3

chessground

Mobile/Web chess UI for lichess.org
TypeScript
974
star
4

mobile

Lichess mobile app v2
Dart
904
star
5

fishnet

Distributed Stockfish analysis for lichess.org
Rust
683
star
6

scalachess

Chess API written in scala. Immutable and free of side effects.
Scala
609
star
7

api

Lichess API documentation and examples
TypeScript
389
star
8

chess-openings

An aggregated data set of chess opening names
Python
315
star
9

stockfish.js

The strong open source chess engine Stockfish compiled to JavaScript and WebAssembly using Emscripten
C++
174
star
10

lila-ws

Lichess' websocket server
Scala
127
star
11

lila-openingexplorer

Opening explorer for lichess.org that can handle all the variants and trillions of unique positions
Rust
122
star
12

compression

Chess clock and move compression algorithms for lichess.org
Java
105
star
13

lila-gif

Webservice to render Gifs of chess positions and games, and stream them frame by frame
Rust
95
star
14

database

Public exports of all rated games, puzzles, and computer evaluations.
Scala
78
star
15

pgn-viewer

PGN viewer widget
TypeScript
75
star
16

lila-tablebase

Tablebase server
Rust
74
star
17

external-engine

Using engines running outside of the browser on https://lichess.org/analysis
Python
68
star
18

berserk

Python client for the lichess API
Python
60
star
19

api-demo

Client-side app using various Lichess APIs for demonstration purpose
TypeScript
50
star
20

kaladin

Machine learning tool aimed at automating cheat detection using insights data.
PureBasic
46
star
21

chessground-examples

Usage examples for chessground
TypeScript
45
star
22

lila-gitpod

Lichess development environment
Shell
39
star
23

lila-search

Keep elasticsearch threads out of your web facing app, kids
Scala
36
star
24

flutter-chessground

Chessboard package for flutter
Dart
32
star
25

lila-http

Handle some https://lichess.org/tournament load
Rust
30
star
26

lila-fishnet

Proxy between lila and fishnet move clients
Scala
30
star
27

dartchess

Dart chess library for native platforms
Dart
29
star
28

lila-docker

Lichess local development using Docker Compose.
Rust
26
star
29

lila-push

Web push microservice for lichess.org
Rust
16
star
30

api-ui

some web UIs on top of the Lichess API
TypeScript
15
star
31

lila-db-seed

Minimum database data for development on ornicar/lila
Python
13
star
32

pgn-mule

Merge, filter, tweak and expose PGN sources
TypeScript
12
star
33

lila-ip2proxy

Webservice to query an IP2Proxy BIN database
Rust
10
star
34

swiss-maker

Script that creates regular swiss tournaments for a team.
TypeScript
8
star
35

lila-maven

sbt resolver 4
Shell
6
star
36

lila-engine

Broker for communication between external engine providers and clients
Rust
6
star
37

leroyjenkins

Follow ban logs to manage ipsets
Rust
6
star
38

broadcaster

Vue
6
star
39

lila-deepq

deepquluru
Rust
4
star
40

lila-stockfish-web

A home for stockfish wasms - because we care
Python
3
star
41

lifat

large assets used by lila in production but optional for dev builds
JavaScript
2
star
42

lila-pwned

Webservice to query https://haveibeenpwned.com/ database dumps
Rust
1
star
43

ab-stub

JavaScript
1
star
44

fishnet-builder

Docker image for an environment that can produce fully static Stockfish builds
C
1
star
45

.github

1
star