Socket
Book a DemoInstallSign in
Socket

capnp-schema-gen

Package Overview
Dependencies
Maintainers
0
Versions
5
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

capnp-schema-gen

A Cap'n Proto schema and setter function generator.

latest
Source
npmnpm
Version
0.5.0
Version published
Maintainers
0
Created
Source

capnp-schema-gen

npm downloads Module type: ESM

A Cap'n Proto schema and setter function generator compatible with Pooya Parsa's capnp-es.

Given an arbitrary JavaScript object:

const data = {
  name: 'John', 
  email: 'john@doe.com', 
  friends: [
    { 
      name: 'Jane',
      email: 'jane@doe.com'
    }
  ]
}

You can generate an inferred Cap'n Proto schema:

const schema = generateSchema('Person', data)

console.log(schema)
@0xe3603ad8685d0f0c;

struct Person {
  name @0 :Text;
  email @1 :Text;
  friends @2 :List(PersonFriendsItem);
}

struct PersonFriendsItem {
  name @0 :Text;
  email @1 :Text;
}

And an accompanying setter function:

const setter = generateSetterFunction(data)

console.log(setter)
function setter(capnp, schemaClass, data) {
  const message = new capnp.Message();
  const obj = message.initRoot(schemaClass);
  if (typeof data.name !== 'undefined') {
    obj.name = data.name;
  }
  if (typeof data.email !== 'undefined') {
    obj.email = data.email;
  }
  const friendsList = obj._initFriends(data.friends.length);
  for (let i = 0; i < data.friends.length; i++) {
    if (data.friends[i] !== null && typeof data.friends[i] === 'object') {
      const friendsItem = friendsList.get(i);
      if (typeof data.friends[i].name !== 'undefined') {
        friendsItem.name = data.friends[i].name;
      }
      if (typeof data.friends[i].email !== 'undefined') {
        friendsItem.email = data.friends[i].email;
      }
    }
  }
  return message.toArrayBuffer();
}

License

MIT

FAQs

Package last updated on 15 Mar 2025

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

About

Packages

Stay in touch

Get open source security insights delivered straight into your inbox.

  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc

U.S. Patent No. 12,346,443 & 12,314,394. Other pending.