{"id":13817034,"url":"https://github.com/piotr-oles/as-proto","last_synced_at":"2025-03-17T03:31:36.347Z","repository":{"id":40695429,"uuid":"427734670","full_name":"piotr-oles/as-proto","owner":"piotr-oles","description":"Protobuf implementation in AssemblyScript","archived":false,"fork":false,"pushed_at":"2023-10-02T14:17:09.000Z","size":341,"stargazers_count":38,"open_issues_count":7,"forks_count":14,"subscribers_count":2,"default_branch":"main","last_synced_at":"2025-03-11T18:53:42.869Z","etag":null,"topics":["assemblyscript","protobuf","typescript","webassembly"],"latest_commit_sha":null,"homepage":"","language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/piotr-oles.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":".github/FUNDING.yml","license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null},"funding":{"github":"piotr-oles"}},"created_at":"2021-11-13T17:51:07.000Z","updated_at":"2025-01-27T21:53:08.000Z","dependencies_parsed_at":"2024-01-18T03:48:28.991Z","dependency_job_id":null,"html_url":"https://github.com/piotr-oles/as-proto","commit_stats":{"total_commits":98,"total_committers":8,"mean_commits":12.25,"dds":"0.22448979591836737","last_synced_commit":"9a242cf816738e904bec8bea81a29eac70ef051d"},"previous_names":[],"tags_count":24,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/piotr-oles%2Fas-proto","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/piotr-oles%2Fas-proto/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/piotr-oles%2Fas-proto/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/piotr-oles%2Fas-proto/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/piotr-oles","download_url":"https://codeload.github.com/piotr-oles/as-proto/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":243841207,"owners_count":20356443,"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":["assemblyscript","protobuf","typescript","webassembly"],"created_at":"2024-08-04T06:00:31.905Z","updated_at":"2025-03-17T03:31:36.013Z","avatar_url":"https://github.com/piotr-oles.png","language":"TypeScript","funding_links":["https://github.com/sponsors/piotr-oles"],"categories":["Packages"],"sub_categories":[],"readme":"\u003cdiv align=\"center\"\u003e\n\n\u003cimg width=\"100\" height=\"100\" src=\"media/assemblyscript-logo.svg\" alt=\"AssemblyScript logo\"\u003e\n\u003cimg width=\"100\" height=\"100\" src=\"media/protobuf-logo.svg\" alt=\"Protobuf logo\"\u003e\n\n\u003ch1\u003eas-proto\u003c/h1\u003e\n\u003cp\u003eProtobuf implementation in AssemblyScript\u003c/p\u003e\n\n[![npm](https://img.shields.io/npm/v/as-proto)](https://www.npmjs.com/package/as-proto)\n\n\u003c/div\u003e\n\n## Features\n * Encodes and decodes protobuf messages\n * Generates AssemblyScript files using `protoc` plugin\n * Produces relatively small `.wasm` files\n * Relatively fast, especially for messages that contains only primitive types\n\n## Installation\nThis package requires **Node 10.4+** or modern browser with [WebAssembly][1] support.\nRequires [`protoc`][2] installed for code generation.\n\n```sh\n# with npm\nnpm install --save as-proto\nnpm install --save-dev as-proto-gen\n\n# with yarn\nyarn add as-proto\nyarn add --dev as-proto-gen\n```\n\n## Code generation\nTo generate AssemblyScript file from `.proto` file, use following command:\n```sh\nprotoc --plugin=protoc-gen-as=./node_modules/.bin/as-proto-gen --as_out=. ./file.proto\n```\nThis command will create `./file.ts` file from `./file.proto` file.\n\n\u003cdetails\u003e\n\u003csummary\u003eGenerated code example:\u003c/summary\u003e\n\n```protobuf\n// star-repo-message.proto\nsyntax = \"proto3\";\n\nmessage StarRepoMessage {\n  string author = 1;\n  string repo   = 2;\n}\n```\n```typescript\n// star-repo-message.ts\n\n// Code generated by protoc-gen-as. DO NOT EDIT.\n// Versions:\n//   protoc-gen-as v0.2.5\n//   protoc        v3.21.4\n\nimport { Writer, Reader } from \"as-proto/assembly\";\n\nexport class StarRepoMessage {\n  static encode(message: StarRepoMessage, writer: Writer): void {\n    writer.uint32(10);\n    writer.string(message.author);\n\n    writer.uint32(18);\n    writer.string(message.repo);\n  }\n\n  static decode(reader: Reader, length: i32): StarRepoMessage {\n    const end: usize = length \u003c 0 ? reader.end : reader.ptr + length;\n    const message = new StarRepoMessage();\n\n    while (reader.ptr \u003c end) {\n      const tag = reader.uint32();\n      switch (tag \u003e\u003e\u003e 3) {\n        case 1:\n          message.author = reader.string();\n          break;\n\n        case 2:\n          message.repo = reader.string();\n          break;\n\n        default:\n          reader.skipType(tag \u0026 7);\n          break;\n      }\n    }\n\n    return message;\n  }\n\n  author: string;\n  repo: string;\n\n  constructor(author: string = \"\", repo: string = \"\") {\n    this.author = author;\n    this.repo = repo;\n  }\n}\n```\n\n\u003c/details\u003e\n\n### Helper methods\nIn order to generate helper methods for encoding and decoding a message, pass the `gen-helper-methods` option with the `as_opt` parameter:\n```sh\nprotoc --plugin=protoc-gen-as=./node_modules/.bin/as-proto-gen --as_opt=gen-helper-methods --as_out=. ./file.proto\n```\n\nThis will add the following methods in a generated file:\n```typescript\nexport function encodeStarRepoMessage(message: StarRepoMessage): Uint8Array {\n  return Protobuf.encode(message, StarRepoMessage.encode);\n}\n\nexport function decodeStarRepoMessage(buffer: Uint8Array): StarRepoMessage {\n  return Protobuf.decode\u003cStarRepoMessage\u003e(buffer, StarRepoMessage.decode);\n}\n```\n\n### Dependencies\nThis package will generate messages for all proto dependencies (for example `import \"google/protobuf/timestamp.proto\";`) \nunless you pass `--as_opt=no-gen-dependencies` option.\n\n## Usage\nTo encode and decode protobuf messages, all you need is `Protobuf` class and\ngenerated message class:\n\n```typescript\nimport { Protobuf } from 'as-proto/assembly';\nimport { StarRepoMessage } from './star-repo-message'; // generated file\n\nconst message = new StarRepoMessage('piotr-oles', 'as-proto');\n\n// encode\nconst encoded = Protobuf.encode(message, StarRepoMessage.encode);\nassert(encoded instanceof Uint8Array);\n\n// decode\nconst decoded = Protobuf.decode(encoded, StarRepoMessage.decode);\nassert(decoded instanceof StarRepoMessage);\nassert(decoded.author === 'piotr-oles');\nassert(decoded.repo === 'as-proto');\n```\n\nIf the helper methods were generated, they can be used to reduce boilerplate code:\n\n```typescript\nimport {\n  StarRepoMessage,\n  encodeStarRepoMessage,\n  decodeStarRepoMessage\n} from './star-repo-message'; // generated file\n\nconst message = new StarRepoMessage('piotr-oles', 'as-proto');\n\nconst encoded = encodeStarRepoMessage(message);\nconst decoded = decodeStarRepoMessage(encoded);\n```\n\nCurrently, the package doesn't support GRPC definitions - only basic Protobuf messages.\n\n## Performance\nI used performance benchmark from [`ts-proto`][3] library and added case for `as-proto`.\nThe results on Intel Core i7 2.2 Ghz (MacBook Pro 2015):\n\n```\nbenchmarking encoding performance ...\n\nas-proto x 1,295,297 ops/sec ±0.30% (92 runs sampled)\nprotobuf.js (reflect) x 589,073 ops/sec ±0.27% (88 runs sampled)\nprotobuf.js (static) x 589,866 ops/sec ±1.66% (89 runs sampled)\nJSON (string) x 379,723 ops/sec ±0.30% (95 runs sampled)\nJSON (buffer) x 295,340 ops/sec ±0.26% (93 runs sampled)\ngoogle-protobuf x 338,984 ops/sec ±1.25% (84 runs sampled)\n\n               as-proto was fastest\n  protobuf.js (reflect) was 54.5% ops/sec slower (factor 2.2)\n   protobuf.js (static) was 55.1% ops/sec slower (factor 2.2)\n          JSON (string) was 70.7% ops/sec slower (factor 3.4)\n        google-protobuf was 74.1% ops/sec slower (factor 3.9)\n          JSON (buffer) was 77.2% ops/sec slower (factor 4.4)\n\nbenchmarking decoding performance ...\n\nas-proto x 889,283 ops/sec ±0.51% (94 runs sampled)\nprotobuf.js (reflect) x 1,308,310 ops/sec ±0.24% (95 runs sampled)\nprotobuf.js (static) x 1,375,425 ops/sec ±2.86% (92 runs sampled)\nJSON (string) x 387,722 ops/sec ±0.56% (95 runs sampled)\nJSON (buffer) x 345,785 ops/sec ±0.33% (94 runs sampled)\ngoogle-protobuf x 359,038 ops/sec ±0.32% (94 runs sampled)\n\n   protobuf.js (static) was fastest\n  protobuf.js (reflect) was 2.4% ops/sec slower (factor 1.0)\n               as-proto was 33.8% ops/sec slower (factor 1.5)\n          JSON (string) was 71.2% ops/sec slower (factor 3.5)\n        google-protobuf was 73.2% ops/sec slower (factor 3.7)\n          JSON (buffer) was 74.2% ops/sec slower (factor 3.9)\n\nbenchmarking combined performance ...\n\nas-proto x 548,291 ops/sec ±0.41% (96 runs sampled)\nprotobuf.js (reflect) x 421,963 ops/sec ±1.41% (89 runs sampled)\nprotobuf.js (static) x 439,242 ops/sec ±0.85% (96 runs sampled)\nJSON (string) x 186,513 ops/sec ±0.25% (94 runs sampled)\nJSON (buffer) x 153,775 ops/sec ±0.54% (94 runs sampled)\ngoogle-protobuf x 160,281 ops/sec ±0.46% (91 runs sampled)\n\n               as-proto was fastest\n   protobuf.js (static) was 20.2% ops/sec slower (factor 1.3)\n  protobuf.js (reflect) was 23.8% ops/sec slower (factor 1.3)\n          JSON (string) was 65.9% ops/sec slower (factor 2.9)\n        google-protobuf was 70.8% ops/sec slower (factor 3.4)\n          JSON (buffer) was 72.0% ops/sec slower (factor 3.6)\n```\n\nThe library is slower on decoding mostly because of GC - AssemblyScript provides very simple (and small) GC\nwhich is not as good as V8 GC. The `as-proto` beats JavaScript on decoding when messages contain\nonly primitive values or other messages (no strings and arrays).\n\n## License\nMIT\n\n[1]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/WebAssembly#browser_compatibility\n[2]: https://grpc.io/docs/protoc-installation/\n[3]: https://github.com/stephenh/ts-proto\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fpiotr-oles%2Fas-proto","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fpiotr-oles%2Fas-proto","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fpiotr-oles%2Fas-proto/lists"}