TwirpScript is a Protocol Buffers (Protobuf) RPC framework designed for JavaScript and TypeScript environments. It automates the generation of both client and server code from `.proto` service definitions, facilitating type-safe communication in both browser and Node.js runtimes. The package is currently at version 0.0.72 and maintains an active release cadence with frequent minor updates addressing bug fixes and improvements. A key differentiator is its adherence to the Twirp Wire Protocol (v7) and its focus on minimizing bundle sizes, especially through tree-shaking and a lightweight runtime (2KB for TwirpScript, 37KB for the underlying ProtoScript serialization runtime). This makes it an efficient alternative to larger RPC frameworks, particularly for web applications requiring lean client bundles and strict type enforcement.
npm install twirpscriptVerified import paths — ran on the pinned version, not inferred.
This example demonstrates how to create and use a TwirpScript client to interact with a generated Protobuf RPC service, such as a 'Haberdasher' service.
Rename your configuration file from `.twirp.json` to `proto.config.mjs` and rewrite its content as a JavaScript ES module. For example, `export default { root: "src" };`.Update your `buf.gen.yaml` to change the plugin path from `./node_modules/protoscript/compiler.js` to `./node_modules/protoscript/dist/compiler.js`.
Ensure any server-side logic or client-side interceptors that process HTTP headers are updated to expect and handle lowercase header names.
While the TypeScript compiler correctly handles these imports, be aware that the runtime expects `.js` extensions. No direct code fix is typically needed, but this is an important distinction for module resolution understanding.
Update to version `0.0.71` or newer to ensure correct JSON serialization of Protobuf `Timestamp` and `Duration` well-known types.
Update to `twirpscript` version `0.0.72` or newer, which includes a fix for resolving the compiler path on Windows to support monorepos.
Update to `twirpscript` version `0.0.66` or newer, which includes a fix for this intermittent compilation issue.
For Buf users, update the plugin path in your `buf.gen.yaml` from `./node_modules/protoscript/compiler.js` to `./node_modules/protoscript/dist/compiler.js` (applicable for versions `v0.0.67` and newer).