@creditkarma/thrift-parser

中文文档
2.0.0发布于 2022/06/23Apache-2.0TypeScriptCommonJS零依赖近 30 天下载 3.5万

A parser for Thrift written in TypeScript

原始使用文档
文档目录10 节
  1. Usage
  2. Build
  3. Test
  4. AST Structure
  5. ThriftDocument
  6. ThriftErrors
  7. Thrift Statements
  8. Viewing with ASTExplorer
  9. Contributing
  10. License

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

版本与发布记录

@creditkarma/thrift-parser 共发布 28 个版本,首次发布于 2017/09/26;过去 12 个月发布了 0 个版本。

版本发布日期距今
2.0.0latest2022/06/234 年前
1.2.1-0next2022/06/044 年前
1.2.02019/06/017 年前
1.1.52019/03/027 年前
1.1.42018/07/108 年前
1.1.32018/05/228 年前

关于 @creditkarma/thrift-parser 的常见问题

@creditkarma/thrift-parser 是什么?

A parser for Thrift written in TypeScript

如何安装 @creditkarma/thrift-parser?

在项目目录运行 npm install @creditkarma/thrift-parser,或使用 pnpm add / yarn add / bun add。它还提供命令行工具 thrift-parser,可以用 npx thrift-parser 直接运行。

@creditkarma/thrift-parser 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@creditkarma/thrift-parser 支持 ESM 还是 CommonJS?

以 CommonJS 形式发布,可以直接 require();在 ES Module 项目中也可以用 import 引入(Node.js 会把 module.exports 作为默认导出)。

@creditkarma/thrift-parser 可以商用吗?

@creditkarma/thrift-parser 使用 Apache-2.0 许可。宽松许可:允许商业使用、修改和再分发,并附带明确的专利授权;分发时需保留许可与 NOTICE 文件,修改过的文件要注明改动。以上为概括,不构成法律意见,请以许可原文为准。

@creditkarma/thrift-parser 的下载量有多少?

根据 npm 官方下载统计,@creditkarma/thrift-parser 在 2026/10/11 – 2026/11/09 这 30 天内被下载 35,017 次,日均约 1,167 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@creditkarma/thrift-parser 还在维护吗?

最新版本 2.0.0 发布于 2022/06/23(4 年前);至今共发布 28 个版本,其中过去 12 个月发布了 0 个;首次发布于 2017/09/26。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。