{"id":16641853,"url":"https://github.com/samthor/thrift-tools","last_synced_at":"2025-06-30T08:08:54.979Z","repository":{"id":196296746,"uuid":"695558105","full_name":"samthor/thrift-tools","owner":"samthor","description":"Work with Thrift definitions and data","archived":false,"fork":false,"pushed_at":"2023-12-06T03:06:46.000Z","size":88,"stargazers_count":2,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"main","last_synced_at":"2025-01-18T15:52:52.915Z","etag":null,"topics":["thrift","thrift-protocol"],"latest_commit_sha":null,"homepage":"","language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/samthor.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null}},"created_at":"2023-09-23T14:48:52.000Z","updated_at":"2023-12-04T22:24:08.000Z","dependencies_parsed_at":null,"dependency_job_id":"e1922dd7-755c-4e5a-aac8-7756528a23b8","html_url":"https://github.com/samthor/thrift-tools","commit_stats":null,"previous_names":["samthor/thrift-tools"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/samthor%2Fthrift-tools","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/samthor%2Fthrift-tools/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/samthor%2Fthrift-tools/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/samthor%2Fthrift-tools/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/samthor","download_url":"https://codeload.github.com/samthor/thrift-tools/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":243164962,"owners_count":20246718,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["thrift","thrift-protocol"],"created_at":"2024-10-12T07:48:05.759Z","updated_at":"2025-03-12T05:28:45.775Z","avatar_url":"https://github.com/samthor.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"Work with Thrift-encoded data in JS/TS.\n\nThrift is a lightweight format for encoding data\u0026hellip; yada yada.\nYou're here because you need to work with its format.\n\nThis 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.\nIt doesn't need native code, and is fast, typesafe and easy to use.\n\n⚠️ Originally, this project was written to parse [Parquet's header definition](https://github.com/apache/parquet-format/blob/master/src/main/thrift/parquet.thrift).\nIt does not aim to support _all_ of the Thrift format but features can be requested if you need them.\n\n## Usage\n\nInstall `thrift-tools` from NPM.\nThis package has zero dependencies and does not include any native bindings.\n\n### Generating Code\n\nYou can convert a \".thrift\" file to a helper by running the script:\n\n```bash\nnpx thrift-tools codegen \u003cpath/to/your/definition-file.thrift\u003e\n```\n\nThis will output TS to your console, which you can then save to a file or do further processing on.\nThe output code imports some helpers from this package `thrift-tools`, which you can bundle in a build.\n\nYou can add support for writing the Thrift classes by adding '-w', but this adds a lot of size that you might not need.\n\nYou can also import the parser programatically:\n\n```ts\nimport { renderThrift } from 'thrift-tools/codegen';\nconst out = renderThrift(`struct Foo { 1: string foo; 2: required list\u003ci32\u003e bar; }`);\n// do something with the TS code in \"out\"\n```\n\nThis generates TypeScript.\nIf your project is pure JS, you'll need to rewrite the generated file at this point.\n\n### Using Generated Code\n\nThe generated code will typically contain a number of `class` definitions.\nThe simple example above will export `class Foo` with two properties.\n\nTo read a `Foo` from a buffer, you can import a concrete parser and read from a buffer:\n\n```ts\nimport { Foo } from './your-codegen-code.ts';\nimport { CompactProtocolReader } from 'thrift-tools';\n\nconst buffer = new Uint8Array(/* TODO: get from disk or network */);\n\nconst f = new Foo();\nf.read(new CompactProtocolReader(buffer));\n\n// f is now read from the buffer \\o/\n```\n\nThe reader and generated code are pure JS and will work in the browser, Node, or any other environment.\n\n### Read Thrift Wire Format\n\nIn some cases you might want to read values from the Thrift wire format directly, rather than specifically into a generated class.\nYou can still use `CompactProtocolReader`:\n\n```ts\nimport { CompactProtocolReader } from 'thrift-tools';\n\nconst buffer = new Uint8Array(/* TODO: get from disk or network */);\nconst reader = new CompactProtocolReader(buffer);\n\nconst number = reader.readI32();\nconst double = reader.readDouble();\n```\n\nFor example, the [`Message`](https://github.com/apache/thrift/blob/master/doc/specs/thrift-compact-protocol.md#message) in Thrift is just a number of adjacent types, rather than a `struct` on the wire.\n\n## Code Size\n\nBuilding and bundling [Parquet's header definition](https://github.com/apache/parquet-format/blob/master/src/main/thrift/parquet.thrift), along with the needed boilerplate to read from a buffer, adds about ~16k (~3.5k gzipped) to your bundle.\nThis size mostly comes from retaining all the names of fields.\n\n## A Note On Integers\n\nThis code doesn't use `bigint`, so will explode if you operate on numbers greater than 53 bits (JS' limit).\nIf this _actually_ hurts you in practice let me know.\n\n## Unsupported Features\n\nThese tools don't yet support:\n\n- const in thrift files\n- service definitions\n- typedefs\n- lots of other things\n\nI'm happy to accept requests.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsamthor%2Fthrift-tools","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fsamthor%2Fthrift-tools","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsamthor%2Fthrift-tools/lists"}