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

@flux-control/modbus-schema

v0.4.0

Published

Device-agnostic Effect Schema generators for Modbus register values, with both Effect-native and synchronous APIs.

Readme

@flux-control/modbus-schema

Device-independent Effect 4 schemas for 16-bit Modbus register values. Each factory returns an entry with Effect-based and synchronous operations. All factory wire values use unsigned UInt16 words (0–65535). Signed scaled values use two's-complement conversion within those words. The package also exports an Int16 schema for signed integers (-32768–32767).

For the complete API reference, see the GitHub Pages documentation.

This project is under active development. Its API may change before the 1.0 release.

Install

bun add @flux-control/modbus-schema

Requires effect v4 (^4.0.3) as a peer dependency. Effect v3 is not supported, because v3 and v4 schemas do not interoperate.

Quick start

Effect-native API

import { makeScaledParam } from '@flux-control/modbus-schema';
import { Effect } from 'effect';

const entry = makeScaledParam(0x0102, 0.1, {
  name: 'Maximum Output Frequency',
  unit: 'Hz',
  range: '4.8~599.0',
  default: '50.0',
});

const program = Effect.gen(function* () {
  const value = yield* entry.decode(500);
  console.log(value); // 50.0
  const wire = yield* entry.encode(60.0);
  console.log(wire); // 600
});

Effect.runSync(program);

Synchronous API

import { makeScaledParam } from '@flux-control/modbus-schema';

const entry = makeScaledParam(0x0102, 0.1, {
  name: 'Maximum Output Frequency',
  unit: 'Hz',
  range: '4.8~599.0',
  default: '50.0',
});

const value = entry.decodeSync(500); // 50.0
const wire = entry.encodeSync(60.0); // 600

Factory functions

Each factory returns a ParamEntry with schema, decode, encode, decodeSync, encodeSync, and formatted. The Effect-based operations fail with Schema.SchemaError. The synchronous operations throw Schema.SchemaError on invalid input or failed encoding. decode and decodeSync accept unknown input and validate the wire word. formatted is the schema's domain-value formatter.

| Factory | Description | | ----------------------------------------------------------------- | ----------------------------------------------------------- | | makeParam(register, meta) | Validate and pass through a branded UInt16 value | | makeScaledParam(register, factor, meta, opts?) | Decode as raw * factor; encode as round(value / factor) | | makeSignedScaledParam(register, factor, meta, opts?) | Apply scaling to a signed two's-complement word | | makeEnumParam(register, labels, meta, opts?) | Map known wire codes to string labels and back | | makeBitfieldParam(register, flagsClass, bitLayout, meta, opts?) | Map bit positions to boolean Schema.Class fields | | makeLookupParam(register, labels, fallback, meta, opts?) | Decode codes with a fallback; encoding always fails | | fromConfig(config) | Select a factory using config.kind |

Scaling and domain validation

Pass a non-zero, finite factor to scaled factories. They do not validate the factor when you create an entry. Both scaled factories multiply the decoded wire value by factor. Encoding rounds value / factor to a register word, so values between register steps lose precision. Unsigned scaling checks the resulting word against UInt16. Signed scaling interprets words above 32767 as negative and checks the rounded signed word against -32768–32767 before encoding. The optional opts.domain schema validates decoded and encoded domain values. Without it, the domain uses Schema.Number. The text in meta.range describes a range; it does not enforce one. Define a domain schema to enforce application limits.

Enums, bitfields, and lookups

An enum must have at least one label. makeEnumParam throws during construction if labels is empty. An unknown wire code fails decoding. If several codes share a label, encoding selects the first matching code in Object.entries(labels) order.

For bitfields, supply a Schema.Class with boolean fields and a zero-based bit position for each field. Use positions 0–15; the factory does not validate each position. Gaps are allowed. The returned entry adds patch, a class with optional boolean fields, and merge(base, patch). A missing or undefined patch field keeps the base flag.

Lookup entries use fallback(raw) for codes absent from labels. They are always read-only. A lookup can also accept opts.domain as its target schema.

Set opts.readOnly: true on scaled, signed scaled, enum, or bitfield factories to reject encoding. Bitfield entries still expose patch and merge. makeParam does not accept this option. A UInt16ParamConfig declares readOnly, but fromConfig does not apply it to makeParam.

Declarative configuration and metadata

Use ParamKind with fromConfig to select a factory from a ParamConfig. A typed config returns the corresponding entry type. If an untyped runtime config has an unknown kind, fromConfig returns undefined.

RegisterMeta requires name and unit. Optional default appears in the schema description and displays undefined when omitted. Optional range appears for non-enum entries and displays undefined when omitted; enum descriptions do not include it. Extra metadata keys also appear as text. Read the description with Schema.resolveAnnotations(entry.schema)?.description.

See examples for complete programs:

Development

| Action | Command | | ----------- | ---------------------------- | | Install | bun install | | Type-check | bun run typecheck | | Test | bun run test | | Run example | bun run examples/<name>.ts | | Build | bun run build |

Tests import source files directly. Build before running examples, because they import the package through its dist/ export.

Source layout

index.ts                     — Re-exports all public API from src/
src/
  index.ts                   — Schema engine: factories, config types, fromConfig, wire primitives
  engine.test.ts             — Unit tests for all factories and sync APIs
examples/
  basic.ts                   — Effect + synchronous API usage
  branded-types.ts           — Domain brands with scaled/signed parameters
  from-config.ts             — Declarative ParamConfig dispatch via fromConfig
  bitfield-flags.ts          — Read-modify-write flag registers
  lookup-table.ts            — Decode-only fault/alarm code lookups
  register-map.ts            — Compose multiple entries into a typed snapshot
  error-handling.ts          — Parse error handling in Effect and sync APIs
  extended-meta.ts           — Add metadata to schema descriptions

License

GPL-3.0