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

cpsat-js

v1.0.0

Published

WebAssembly port of Google OR-Tools CP-SAT constraint programming solver

Downloads

408

Readme

cpsat-js

WebAssembly port of Google OR-Tools' CP-SAT constraint programming solver. Runs in browser and Node.js with zero native dependencies.

Install

npm install cpsat-js

Using with Vite

Add cpsat-js to optimizeDeps.exclude in your vite.config.ts:

export default defineConfig({
  optimizeDeps: {
    exclude: ['cpsat-js'],
  },
});

Without this, Vite's dep pre-bundler (esbuild) copies the package into node_modules/.vite/deps/ and breaks the relative new URL('../../build/cpsat.wasm', import.meta.url) lookup — the dev server then returns the SPA HTML fallback for the WASM request, and Emscripten fails with CompileError: expected magic word 00 61 73 6d, found 3c 21 64 6f (<!do... from the HTML). Excluding the package routes it through Vite's main asset pipeline, which rewrites the URL correctly. Production builds (vite build) don't use the pre-bundler and work without this flag, but it's harmless to set in both.

Quick Start

import { CpModel, CpSolver, CpSolverStatus } from 'cpsat-js';

const solver = await CpSolver.create();

// Solve: maximize x + y subject to x + y <= 10
const model = new CpModel();
const x = model.newIntVar(0, 10, 'x');
const y = model.newIntVar(0, 10, 'y');

model.add(x.plus(y).le(10));
model.maximize(x.plus(y));

const result = solver.solve(model);

if (result.status === CpSolverStatus.OPTIMAL) {
  console.log(`x = ${result.value(x)}, y = ${result.value(y)}`);
  console.log(`objective = ${result.objectiveValue}`);
}

Supported Constraints

  • Linear constraints (add(expr.le(val)), add(expr.ge(val)), add(expr.equals(val)))
  • addAllDifferent([...vars]) — forces all variables to take distinct values
  • addBoolOr([...literals]) / addBoolAnd([...literals]) — boolean logic
  • addNoOverlap([...intervals]) — scheduling / disjunctive constraints
  • addCircuit([[tail, head, literal], ...]) — routing / TSP
  • minimize(expr) / maximize(expr) — optimization objectives
  • .onlyEnforceIf(literal) — conditional enforcement (half-reification)

Examples

Knapsack

const model = new CpModel();
const items = [[60, 10], [100, 20], [120, 30]]; // [value, weight]
const capacity = 50;

const take = items.map((_, i) => model.newBoolVar(`take_${i}`));
const weight = take.reduce((acc, t, i) => acc.plus(t.times(items[i][1])), take[0].times(0));
const value = take.reduce((acc, t, i) => acc.plus(t.times(items[i][0])), take[0].times(0));

model.add(weight.le(capacity));
model.maximize(value);

const result = solver.solve(model);

N-Queens

const n = 8;
const model = new CpModel();
const queens = Array.from({ length: n }, (_, i) => model.newIntVar(0, n - 1, `q${i}`));

model.addAllDifferent(queens);
model.addAllDifferent(queens.map((q, i) => q.plus(i)));   // diagonals
model.addAllDifferent(queens.map((q, i) => q.minus(i)));  // anti-diagonals

const result = solver.solve(model);
const cols = queens.map(q => result.value(q));

Scheduling

const model = new CpModel();
const durations = [3, 5, 2];
const horizon = 10;

const starts = durations.map((_, i) => model.newIntVar(0, horizon, `s${i}`));
const ends = durations.map((_, i) => model.newIntVar(0, horizon, `e${i}`));
const intervals = durations.map((d, i) =>
  model.newIntervalVar(starts[i], d, ends[i], `task${i}`),
);

model.addNoOverlap(intervals);

const makespan = model.newIntVar(0, horizon, 'makespan');
ends.forEach(e => model.add(makespan.ge(e)));
model.minimize(makespan);

const result = solver.solve(model);

API Reference

CpModel

Builder for constraint programming models.

  • newIntVar(lb, ub, name): IntVar
  • newBoolVar(name): BoolVar
  • newConstant(value): IntVar
  • newIntervalVar(start, size, end, name): IntervalVar
  • add(boundedExpr): Constraint — adds a linear constraint like x.plus(y).le(10)
  • addAllDifferent(vars): Constraint
  • addBoolOr(literals): Constraint
  • addBoolAnd(literals): Constraint
  • addNoOverlap(intervals): Constraint
  • addCircuit(arcs): Constraint
  • minimize(expr) / maximize(expr)

IntVar / BoolVar

Expression building methods:

  • plus(other), minus(other), times(scalar), negate()
  • equals(other), le(other), ge(other), lt(other), gt(other), notEquals(other)
  • not() — returns the negative literal (for boolean vars)

CpSolver

  • static create(options?): Promise<CpSolver> — async factory that loads WASM
  • solve(model, params?): CpSolverResult

CpSolverResult

  • status: CpSolverStatus (UNKNOWN, MODEL_INVALID, FEASIBLE, INFEASIBLE, OPTIMAL)
  • objectiveValue: number
  • bestObjectiveBound: number
  • wallTime: number (seconds)
  • value(variable): number — solution value
  • response: CpSolverResponse — raw protobuf response

Architecture

  • TypeScript wrapper builds a CpModelProto via a fluent API
  • Protobuf serialization (@bufbuild/protobuf) is the JS↔WASM boundary
  • Single WASM exportsolve(proto_bytes) → response_bytes
  • CP-SAT core (OR-Tools) runs inside WebAssembly

Building from Source

Requires CMake 3.18+, Ninja, and Emscripten.

npm install
npm run build:proto    # Generate TS types from .proto
npm run build:wasm     # Compile CP-SAT to WASM (slow: ~20min first time)
npm run build:ts       # Compile TypeScript
npm test               # Run unit + integration tests

License

Apache 2.0 (same as OR-Tools).

Credits