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 🙏

© 2024 – Pkg Stats / Ryan Hefner

@creditkarma/thrift-parser

v2.0.0

Published

A parser for Thrift written in TypeScript

Downloads

42,746

Readme

TypeScript Thrift Parser

A parser for Thrift written in TypeScript. The resulting AST can be used to codegen JavaScript from a Thrift file, or just to inspect the Thrift structure.

Usage

A successful parse returns a ThriftDocument object. An unsuccessful parse returns a ThriftErrors object.

import { parse, ThriftDocument } from '@creditkarma/thrift-parser'


const rawThrift: string =`
  struct MyStruct {
    1: required i32 id
  }
`;

const thriftAST: ThriftDocument | ThriftErrors = parse(rawThrift);

switch(thriftAST.type) {
  case 'ThriftDocument':
    // Do something with valid AST
  case 'ThriftErrors':
    // Report or recover from errors
}

You can also use Thrift Parser from the command line or npm scripts. When using from the command line the generated AST is saved to file as JSON.

$ thrift-parser --rootDir thrift --outDir thrift-json --fastFail false some_file.thrift

In this usage there are three options:

  • --rootDir: where to initiate file search and save
  • --outDir: relative to rootDir where to save output files
  • --fastFail: If true fail on first error encountered

Build

$ npm install
$ npm run build

Test

$ npm test

AST Structure

The root of the returned AST is either a ThriftDocument (successful parse) or a ThriftErrors (unsuccessful parse).

ThriftDocument

{
  type: "ThriftDocument",
  body: Array<ThriftStatement>
}

ThriftErrors

{
  type: "ThriftErrors",
  errors: Array<ThriftError>
}

ThriftError

A descriptor of what went wrong while parsing the specified Thrift source.

{
  type: "ParseError" | "ScanError",
  message: string,
  loc: TextLocation
}

Thrift Statements

Thrift Statements represent each of the main constructs that can be defined in Thrift source.

NamespaceDefinition

namespace <identifier> <identifier>
{
  type: "NamespaceDefinition",
  scope: Identifier,
  name: Identifier
}

IncludeDefinition

include '<path>'"
{
  type: "IncludeDefinition",
  path: StringLiteral
}

TypedefDefinition

typedef <field-type> <identifier>
{
  type: "TypedefDefinition",
  name: Identifier,
  definitionType: FieldType
}

ConstDefinition

const <field-type> <identifier> = <initializer>
{
  type: "ConstDefinition",
  name: Identifier,
  fieldType: FieldType,
  initializer: ConstValue,
}

EnumDefinition

enum <identifier> { <members> }
{
  type: "EnumDefinition",
  name: Identifier,
  members: Array<EnumMember>
}

StructDefinition

struct <identifier> { <fields> }
{
  type: "StructDefinition",
  name: Identifier,
  fields: Array<FieldDefinition>
}

UnionDefinition

union <identifier> { <fields> }
{
  type: "UnionDefinition",
  name: Identifier,
  fields: Array<FieldDefinition>
}

ExceptionDefinition

exception <identifier> { <fields> }
{
  type: "ExceptionDefinition",
  name: Identifier,
  fields: Array<FieldDefinition>
}

ServiceDefinition

service <identifier> (extends <identifier>)? { <functions> }
{
  type: "ServiceDefinition",
  name: Identifier,
  extends: Identifier | null,
  functions: Array<FunctionDefinition>
}

Viewing with ASTExplorer

ASTExplorer is a web app for visualizing ASTs. You type source. It shows you the resulting syntax tree based on the parser you've selected. I've included the integrations for this parser. To get that up and running you'll need to clone ASTExplorer.

$ git clone https://github.com/fkling/astexplorer.git
$ cd astexplorer/website
$ npm install

You will now need to install thrift-parser for ASTExplorer

$ npm install @creditkarma/thrift-parser

Cool, now we need to copy some stuff into the ASTExplorer project.

If the ASTExplorer project and the @creditkarma/thrift-parser project are siblings you can type this into the temrinal (from astexplorer/website)...

$ cp -r ../../thrift-parser/astexplorer/thrift ./src/parsers/thrift

You'll now need to build ASTExplorer and start the server

$ npm run build
$ npm start

By default this will start ASTExplorer on localhost:8080

There is a dropdown to select the language you want to use, choose 'Thrift IDL'

Note: I have had some trouble getting npm run build to work. However, the watch build is much more reliable.

$ npm run watch

Then in another terminal window run start.

$ npm start

Contributing

For more information about contributing new features and bug fixes, see our Contribution Guidelines. External contributors must sign Contributor License Agreement (CLA)

License

This project is licensed under Apache License Version 2.0