{"id":13511887,"url":"https://github.com/mafintosh/protocol-buffers","last_synced_at":"2025-05-14T18:04:11.762Z","repository":{"id":16712974,"uuid":"19469852","full_name":"mafintosh/protocol-buffers","owner":"mafintosh","description":"Protocol Buffers for Node.js","archived":false,"fork":false,"pushed_at":"2024-06-14T07:47:59.000Z","size":164,"stargazers_count":762,"open_issues_count":29,"forks_count":75,"subscribers_count":13,"default_branch":"master","last_synced_at":"2025-04-10T04:53:45.738Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"","language":"JavaScript","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/mafintosh.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":"SECURITY.md","support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2014-05-05T20:20:39.000Z","updated_at":"2024-10-27T20:27:54.000Z","dependencies_parsed_at":"2024-06-18T12:26:24.456Z","dependency_job_id":null,"html_url":"https://github.com/mafintosh/protocol-buffers","commit_stats":{"total_commits":202,"total_committers":17,"mean_commits":"11.882352941176471","dds":"0.11386138613861385","last_synced_commit":"f95f5c1bb892b655508eadb3a8e5cf8429bc5ae2"},"previous_names":[],"tags_count":65,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mafintosh%2Fprotocol-buffers","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mafintosh%2Fprotocol-buffers/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mafintosh%2Fprotocol-buffers/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mafintosh%2Fprotocol-buffers/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/mafintosh","download_url":"https://codeload.github.com/mafintosh/protocol-buffers/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":254198452,"owners_count":22030964,"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":[],"created_at":"2024-08-01T03:01:16.737Z","updated_at":"2025-05-14T18:04:06.754Z","avatar_url":"https://github.com/mafintosh.png","language":"JavaScript","readme":"# protocol-buffers\n\n[Protocol Buffers](https://developers.google.com/protocol-buffers/) for Node.js\n\n```\nnpm install protocol-buffers\n```\n\n[![build status](https://github.com/mafintosh/protocol-buffers/actions/workflows/test.yml/badge.svg)](https://github.com/mafintosh/protocol-buffers/actions/workflows/test.yml)\n![dat](http://img.shields.io/badge/Development%20sponsored%20by-dat-green.svg?style=flat)\n\n## Usage\n\nAssuming the following `test.proto` file exists\n\n```proto\nenum FOO {\n  BAR = 1;\n}\n\nmessage Test {\n  required float num  = 1;\n  required string payload = 2;\n}\n\nmessage AnotherOne {\n  repeated FOO list = 1;\n}\n```\n\nUse the above proto file to encode/decode messages by doing\n\n``` js\nvar protobuf = require('protocol-buffers')\n\n// pass a proto file as a buffer/string or pass a parsed protobuf-schema object\nvar messages = protobuf(fs.readFileSync('test.proto'))\n\nvar buf = messages.Test.encode({\n  num: 42,\n  payload: 'hello world'\n})\n\nconsole.log(buf) // should print a buffer\n```\n\nTo decode a message use `Test.decode`\n\n``` js\nvar obj = messages.Test.decode(buf)\nconsole.log(obj) // should print an object similar to above\n```\n\nEnums are accessed in the same way as messages\n\n``` js\nvar buf = messages.AnotherOne.encode({\n  list: [\n    messages.FOO.BAR\n  ]\n})\n```\n\nNested emums are accessed as properties on the corresponding message\n\n``` js\nvar buf = message.SomeMessage.encode({\n  list: [\n    messages.SomeMessage.NESTED_ENUM.VALUE\n  ]\n})\n```\n\nSee the [Google Protocol Buffers docs](https://developers.google.com/protocol-buffers/) for more information about the\navailable types etc.\n\n## Compile to a file\n\nSince v4 you can now compile your schemas to a JavaScript file you can require from Node.\nThis means you do not have runtime parse the schemas, which is useful if using in the browser or on embedded devices.\nIt also makes the dependency footprint a lot smaller.\n\n``` sh\n# first install the cli tool\nnpm install -g protocol-buffers\n\n# compile the schema\nprotocol-buffers test.proto -o messages.js\n\n# then install the runtime dependency in the project\nnpm install --save protocol-buffers-encodings\n```\n\nThat's it! Then in your application you can simply do\n\n``` js\nvar messages = require('./messages')\n\nvar buf = messages.Test.encode({\n  num: 42\n})\n```\n\nThe compilation functionality is also available as a JavaScript API for programmatic use:\n\n``` js\nvar protobuf = require('protocol-buffers')\n\n// protobuf.toJS() takes the same arguments as protobuf()\nvar js = protobuf.toJS(fs.readFileSync('test.proto'))\nfs.writeFileSync('messages.js', js)\n```\n\n## Imports\n\nThe cli tool supports protocol buffer [imports][] by default.\n\n**Currently all imports are treated as public and the public/weak keywords\nnot supported.**\n\nTo use it programmatically you need to pass-in a `filename` \u0026 a `resolveImport`\nhooks:\n\n```js\nvar protobuf = require('protocol-buffers')\nvar messages = protobuf(null, {\n  filename: 'initial.proto',\n  resolveImport (filename) {\n    // can return a Buffer, String or Schema\n  }\n})\n```\n\n[imports]: https://developers.google.com/protocol-buffers/docs/proto3#importing_definitions\n\n## Performance\n\nThis module is fast.\n\nIt uses code generation to build as fast as possible encoders/decoders for the protobuf schema.\nYou can run the benchmarks yourself by doing `npm run bench`.\n\nOn my Macbook Air it gives the following results\n\n```\nBenchmarking JSON (baseline)\n  Running object encoding benchmark...\n  Encoded 1000000 objects in 2142 ms (466853 enc/s)\n\n  Running object decoding benchmark...\n  Decoded 1000000 objects in 970 ms (1030928 dec/s)\n\n  Running object encoding+decoding benchmark...\n  Encoded+decoded 1000000 objects in 3131 ms (319387 enc+dec/s)\n\nBenchmarking protocol-buffers\n  Running object encoding benchmark...\n  Encoded 1000000 objects in 2089 ms (478698 enc/s)\n\n  Running object decoding benchmark...\n  Decoded 1000000 objects in 735 ms (1360544 dec/s)\n\n  Running object encoding+decoding benchmark...\n  Encoded+decoded 1000000 objects in 2826 ms (353857 enc+dec/s)\n```\n\nNote that JSON parsing/serialization in node is a native function that is *really* fast.\n\n## Leveldb encoding compatibility\n\nCompiled protocol buffers messages are valid levelup encodings.\nThis means you can pass them as `valueEncoding` and `keyEncoding`.\n\n``` js\nvar level = require('level')\nvar db = level('db')\n\ndb.put('hello', {payload:'world'}, {valueEncoding:messages.Test}, function(err) {\n  db.get('hello', {valueEncoding:messages.Test}, function(err, message) {\n    console.log(message)\n  })\n})\n```\n\n## License\n\nMIT\n","funding_links":[],"categories":["JavaScript","Protocol Buffers"],"sub_categories":["React Components"],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmafintosh%2Fprotocol-buffers","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmafintosh%2Fprotocol-buffers","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmafintosh%2Fprotocol-buffers/lists"}