Giter VIP home page Giter VIP logo

thrift-tools's Introduction

Work with Thrift-encoded data in JS/TS.

Thrift is a lightweight format for encoding data… yada yada. You're here because you need to work with its format.

This package includes a code generator that builds TypeScript classes from ".thrift" definition files, and helpers to read the compact binary Thrift format into those classes so you can get your job done. It doesn't need native code, and is fast, typesafe and easy to use.

⚠️ Originally, this project was written to parse Parquet's header definition. It does not aim to support all of the Thrift format but features can be requested if you need them.

Usage

Install thrift-tools from NPM. This package has zero dependencies and does not include any native bindings.

Generating Code

You can convert a ".thrift" file to a helper by running the script:

npx thrift-tools codegen <path/to/your/definition-file.thrift>

This will output TS to your console, which you can then save to a file or do further processing on. The output code imports some helpers from this package thrift-tools, which you can bundle in a build.

You can add support for writing the Thrift classes by adding '-w', but this adds a lot of size that you might not need.

You can also import the parser programatically:

import { renderThrift } from 'thrift-tools/codegen';
const out = renderThrift(`struct Foo { 1: string foo; 2: required list<i32> bar; }`);
// do something with the TS code in "out"

This generates TypeScript. If your project is pure JS, you'll need to rewrite the generated file at this point.

Using Generated Code

The generated code will typically contain a number of class definitions. The simple example above will export class Foo with two properties.

To read a Foo from a buffer, you can import a concrete parser and read from a buffer:

import { Foo } from './your-codegen-code.ts';
import { CompactProtocolReader } from 'thrift-tools';

const buffer = new Uint8Array(/* TODO: get from disk or network */);

const f = new Foo();
f.read(new CompactProtocolReader(buffer));

// f is now read from the buffer \o/

The reader and generated code are pure JS and will work in the browser, Node, or any other environment.

Read Thrift Wire Format

In some cases you might want to read values from the Thrift wire format directly, rather than specifically into a generated class. You can still use CompactProtocolReader:

import { CompactProtocolReader } from 'thrift-tools';

const buffer = new Uint8Array(/* TODO: get from disk or network */);
const reader = new CompactProtocolReader(buffer);

const number = reader.readI32();
const double = reader.readDouble();

For example, the Message in Thrift is just a number of adjacent types, rather than a struct on the wire.

Code Size

Building and bundling Parquet's header definition, along with the needed boilerplate to read from a buffer, adds about ~16k (~3.5k gzipped) to your bundle. This size mostly comes from retaining all the names of fields.

A Note On Integers

This code doesn't use bigint, so will explode if you operate on numbers greater than 53 bits (JS' limit). If this actually hurts you in practice let me know.

Unsupported Features

These tools don't yet support:

  • const in thrift files
  • service definitions
  • typedefs
  • lots of other things

I'm happy to accept requests.

thrift-tools's People

Contributors

samthor avatar

Stargazers

 avatar  avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.