New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

kiwi-schema

Package Overview
Dependencies
Maintainers
1
Versions
52
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

kiwi-schema

This is a binary encoding format inspired by Google's [Protocol Buffer](https://developers.google.com/protocol-buffers/) format.

  • 0.0.7
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
5.7K
decreased by-28.51%
Maintainers
1
Weekly downloads
 
Created
Source

Kiwi Message Format

This is a binary encoding format inspired by Google's Protocol Buffer format.

Native Types

  • bool: A value that stores either true or false. Will use 1 byte.
  • byte: An unsigned 8-bit integer value. Uses 1 byte, obviously.
  • int: A 32-bit integer value stored using a variable-length encoding optimized for storing numbers with a small magnitude. Will use at most 5 bytes.
  • uint: A 32-bit integer value stored using a variable-length encoding optimized for storing small non-negative numbers. Will use at most 5 bytes.
  • float: A 32-bit floating-point number. Will always use 4 bytes.
  • string: A UTF-8 length-prefixed string. Will use at least 1 byte.
  • T[]: Any type can be made into an array using the [] suffix.

User Types

  • enum: A uint with a restricted set of values that are identified by name.
  • struct: A compound value with a fixed set of fields that are always required and written out in order.
  • message: A compound value with optional fields. A field can be made required using the required keyword.

Example Schema

enum Type {
  FLAT = 0;
  ROUND = 1;
  POINTED = 2;
}

struct Color {
  byte red;
  byte green;
  byte blue;
  byte alpha;
}

message RootMessage {
  required uint clientID = 1;
  Type type = 2;
  Color[] colors = 3;
}

Differences from Protocol Buffers

  • Kiwi adds support for efficient compound messages using the struct keyword
  • Enums are scoped to their type instead of dumping everything into the global scope like C
  • It's always possible to check for field presence, even for fields that hold arrays
  • The generated C++ code is a lot simpler and only depends on a single file, kiwi.h

JavaScript usage

Make sure to install the kiwi package beforehand using npm install kiwi-schema.

var kiwi = require('kiwi-schema');
var schema = kiwi.compileSchema([
  'message Test {',
  '  int x = 1;',
  '}',
].join('\n'));

var buffer = schema.encodeTest({x: 123});
var test = schema.decodeTest(buffer);

if (test.x !== undefined) {
  console.log('x is %d', test.x);
}

C++ usage

Make sure to generate the C++ code beforehand using something like kiwic --schema test.kiwi --cpp test.h.

#include <stdio.h>
#include "test.h"

int main() {
  Test test;
  test.set_x(123);

  kiwi::ByteBuffer buffer;
  bool encode_success = test.encode(buffer);

  Test test2;
  kiwi::MemoryPool pool;
  bool decode_success = test2.decode(buffer, pool);

  if (test2.x()) {
    printf("x is %d\n", *test2.x());
  }

  return 0;
}

FAQs

Package last updated on 22 Apr 2016

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

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc