Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

capnp-es

unjs2.7kMIT0.0.7TypeScript support: included

TypeScript implementation of the Cap'n Proto serialization.

readme

🔥 capnp-es

npm version npm downloads bundle size codecov

[!WARNING] This is an alpha-quality software. please use at your own risk (project status).

TypeScript implementation of the Cap'n Proto serialization protocol.

Cap’n Proto is an insanely fast data interchange format and capability-based RPC system. Think JSON, except binary. Or think Protocol Buffers, except faster. Cap’n Proto was built by Kenton Varda to be used in Sandstorm and is now heavily used in Cloudflare.

Usage

Compiling schema

[!NOTE] Make sure capnpc command is available. You can find install instructions here to install it.

Install capnp-es dependency:

npx nypm install capnp-es

You can use capnp-es to compile a schema file into typeScript/javascript source code:

npx capnp-es path/to/myschema.capnp -ojs,ts,dts

This will generate path/to/myschema.{js,ts,dts}.

Use npx capnp-es --help for full usage info.

See playground for examples and learn more about .capnp schema in language docs.

Reading Messages

Here's a quick usage example:

import * as capnp from "capnp-es";
import { MyStruct } from "./myschema.js";

const message = capnp.Message.fromArrayBuffer(buffer);
const struct = message.getRoot(MyStruct);

RPC Protocol

Experimental RPC protocol is supported (level 1).

See tests for some examples.

Status

This project is a rework of jdiaz5513/capnp-ts by Julián Díaz and is under development.

<summary>Changes from capnp-ts</summary>
  • Internal refactors and simplifications as was playing around.
  • Compiler, runtime, and std lib published via a single and compact ESM-only package with subpath exports.
  • Compiler updated to use Typescript v5 API
  • Output files can be .ts (new), .js (ESM instead of CJS), and .d.ts and has no .capnp suffix.
  • Compiler API can be used via the capnp-es/compiler subpath export programmatically.
  • Use native TextEncoder and TextDecoder for utf8 encoding
  • Enums are typed plain JS objects (this way .ts files work with strip-only ts loaders without enum support.)
  • Compiler CLI can directly accept a path to .capnp files and internally use capnpc
  • Built-in schemas are compiled from source (compiler, compiles itself. so cool right?)
  • Use reflection (getter setters) to access structs.
  • RPC level-1 merged from jdiaz5513/capnp-ts#169.
  • List interface implements Array object (custom methods removed).
  • Pointers had been improved to feel (inspected and serialized) like native JS values as much as possible.
  • Basic JSDocs generated for class and getter

Contribution

Feedback and PRs are more than welcome. 🙏

<summary>Local development</summary>
  • Clone this repository
  • Install the latest LTS version of Node.js
  • Enable Corepack using corepack enable
  • Install dependencies using pnpm install
  • Run interactive tests using pnpm dev

License

🔀 Forked from jdiaz5513/capnp-ts by Julián Díaz.

💛 Published under the MIT license.

changelog

Changelog

v0.0.7

compare changes

📦 Build

❤️ Contributors

  • Pooya Parsa (@pi0)

v0.0.6

compare changes

🩹 Fixes

  • rpc: Conditionally use FinalizationRegistry (#7)

📖 Documentation

  • Update to use local install (#9)

🏡 Chore

❤️ Contributors

  • Pooya Parsa (@pi0)

v0.0.5

compare changes

🩹 Fixes

  • Various fixes (#10)

🏡 Chore

❤️ Contributors

v0.0.4

compare changes

🚀 Enhancements

  • cli: Allow passing more options (f794de7)
  • Prefix _init, _is, _disown and _has (1cbf9d1)

🩹 Fixes

  • Fix source info matcher (ff0bd2f)
  • cli: Try to guess absolute paths (06730e1)

📦 Build

  • Fully auto generate std files (e5b2eda)

🏡 Chore

❤️ Contributors

  • Pooya Parsa (@pi0)

v0.0.3

compare changes

🚀 Enhancements

  • compiler: Enums as typed plain objects (d316d8b)
  • cli: Allow directly passing path to capnp files (7eb8f86)
  • Use getter/setter for struct (88504dd)
  • Compile schemas from source (1d7bf84)
  • Experimental RPC level 1 (#3)
  • Support iterator lists (bbd512a)
  • Implement Array interface for List (8e80a25)
  • Improve inspection of pointers (1fd6341)
  • Generate basic jsdocs (1be83a7)

🔥 Performance

  • Use native TextEncoder and `TextDncoder for text (1b41d55)

🩹 Fixes

💅 Refactors

  • Shorter code gen (1feaf4a)
  • Better src/pointers structure (39d7049)
  • Rename std to capnp (7abfafc)
  • Decouple struct and pointer utils from class (b19e4e1)

📦 Build

  • Mark typescript as optional peer dependency (7b67d8c)

🏡 Chore

✅ Tests

  • Run compile e2e within vitest (96cf7f5)

🤖 CI

❤️ Contributors

  • Pooya Parsa (@pi0)

v0.0.2

compare changes

📦 Build

  • Mark typescript as optional peer dependency (7b67d8c)

🏡 Chore

❤️ Contributors

  • Pooya Parsa (@pi0)

v0.0.1

🏡 Chore

🤖 CI

❤️ Contributors

  • Pooya Parsa (@pi0)